> ## Content Index
> Fetch the complete content index at: https://www.bambooweekly.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Bamboo Weekly #74: UK elections
- URL: https://www.bambooweekly.com/bw-74-uk-elections/
- Published: 2024-07-10T15:00:55.000Z
- Updated: 2026-08-06T10:45:13.000Z
- Description: Get better at: Excel files, multiple files, pivot tables, plotting, and missing data.
- Author: Reuven M. Lerner
- Tags: excel, multiple-files, pivot-table, plotting, missing-data

*\[I'm at Euro Python in Prague this week. If you're attending, then please find me and say "hi"!\]*

There are a lot of elections going on this year. Some of them, like those in India and the United States, were long expected. Others, such as those in the United Kingdom and France, came as a surprise. Granted, the UK was going to have elections at some point in the coming months, but Rishi Sunak's call for an election on July 4th came as a surprise.

It was particularly surprising given how dismally the ruling Conservative party had been polling for quite some time; why would Sunak call an early election that he was almost certainly going to lose? And lose he did; the Labour party swept into power with numbers that haven't been seen in a long time. (For some background on the UK's election, I recommend John Oliver's [recent "Last Week Tonight" episode](https://www.youtube.com/watch?v=tkAqwHiAR-g&ref=bambooweekly.com). I promise, it's worth watching until the very end.)

Democracies are generally more transparent than autocracies, and this is especially true when it comes to elections. It's now common for countries to make election data downloadable in CSV or similar format for analysis. You can download data from India's recent elections, in which Narendra Modi's BJP will have to form a coalition government. You can download data from the first round of French parliamentary elections, in which it appeared as though the right-wing National Rally was going to win; the second round, for which data isn't yet available, had major gains for the left and center, but it's not clear how they'll rule. Maybe they can take some lessons from the Netherlands, which has a government after many months of coalition negotiations.

It's all very messy, but it's still better to have elections than not, and to have the data available for public review than not.

I'm far from an expert in UK politics, but this was an interesting and highly anticipated election to watch. And with rich data readily available, I thought it would be interesting to look at the results -- along the way, perhaps learning a bit more about the UK, its parliament and parties, and (of course) the winners and losers in the most recent election.

### Data and seven questions

This week's data comes from the House of Commons library, part of the UK's Parliament. The full research briefing, including charts and graphs, can be read at

[https://commonslibrary.parliament.uk/research-briefings/cbp-10009/](https://commonslibrary.parliament.uk/research-briefings/cbp-10009/?ref=bambooweekly.com)

They provide two Excel spreadsheets, one describing the winners of the recent elections, split up per electoral district (known as a "constituency"). A similar, companion document, lists the members who were defeated. You can download them from here:

[https://researchbriefings.files.parliament.uk/documents/CBP-10009/Winning-members.xlsx](https://researchbriefings.files.parliament.uk/documents/CBP-10009/Winning-members.xlsx?ref=bambooweekly.com)  
[https://researchbriefings.files.parliament.uk/documents/CBP-10009/Defeated-MPs.xlsx](https://researchbriefings.files.parliament.uk/documents/CBP-10009/Defeated-MPs.xlsx?ref=bambooweekly.com)

This week's learning goals include joins, groups, pivot tables, plotting, and working with strings.

I'll be back tomorrow with my full solutions and explanations, as well as the Jupyter notebook I used to solve these problems. Meanwhile, here are my six challenges:

- Load the two files into a single data frame, with one row for each constituency, and the index being the `ons_id` columns from each. Columns from the defeated file should have the `_defeated` suffix attached to their names.
- Which party won the greatest number of votes in each region? Which party lost the greatest number of seats in each region?
- Produce a pie chart showing how many seats were won by each political party. Use some of the traditional colors (er, colours) for the parties' pie slices: Red for Labour, blue for the Conservatives, and orange for the Liberal Democrats. You should probably choose another few colors just to make the pie chart look a bit more interesting.
- What is the proportion of women in the incoming parliament? In how many races did a female candidate defeat a male candidate, and vice versa?
- The new prime minister is Sir Keir Starmer. How many members of parliament have the title of "Sir" or "Dame", and how many of them are from each party?
- Does a shorter, snappier name help someone to win an election? For races in which a sitting MP was defeated, in how many cases did the newcomer have a shorter name? (To determine which is "shorter," combine the lengths of the first, middle, and last names.)

Until tomorrow,

Reuven