BW #74: UK elections
[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. 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/
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/Defeated-MPs.xlsx
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?