Messing around with Elections NI data

Sources:

Creating your own Google Sheet and referencing the crowdsourced data

The above linked spreadsheets are naturally not editable by everyone; this is great for reliable data but isn’t so great when you want to make pretty graphs.

Google Sheets supports the live referencing of external sheets in your own sheets, so you can ‘import’ the data from the read-only sheets as they evolve over the count, and then reference those data in your own visualisations.

This is done using the IMPORTRANGE function in Google Sheets, so like this;

=IMPORTRANGE("https://docs.google.com/spreadsheets/d/1AazeIZwfflJJoTiYNil3RprIOXcNy8yyzfZ4ImVlETA/edit#gid=372848906","Belfast South/West!A2:Q24")

Image of Imported Spreadsheet showing separated results for the Belfast South Assembly Election in 2022

Using this and the Google QUERY language, you can easily create some pretty dynamic graphs in a couple of lines/cells across your own sheets, all while being ‘fed’ by the main collaborative work.

Image of PieChart of First Preference Votes in Belfast South in 2022

For instance, this is generated from the following formula;

=query('Basic IMPORTRANGE'!A2:Q16, "select B, sum(C) group by B order by sum(C) desc");

In this case the first argument to the query is a reference to my own sheet that just has ‘IMPORTRANGE’ in it; the interesting bit is the second argument which gives a list of the values in column B (The Party Names in the count sheet) sorted by the sum of the matching rows in column C (the first preference votes), with that ‘sum’ being defined across the groups with the same values in column B, and finally, these all sorted in a descending fashion by the total of those first preference votes.

So now we’ve easily set up a aggregation with two ‘cells’ of formula and a fairly basic chart.

Doing the real work with Python

Python is really powerful for this kind of stuff, particularly the pandas data management library; we can get the above referenced sheet into a ‘raw’ python format with just a ‘few lines of code’.

import pandas as pd
from urllib.parse import quote

sheet_id = "1AazeIZwfflJJoTiYNil3RprIOXcNy8yyzfZ4ImVlETA" # This is the bit taken from the URL above, like IMPORTRANGE above
tab_id = 372848906
# https://docs.google.com/spreadsheets/d/1AazeIZwfflJJoTiYNil3RprIOXcNy8yyzfZ4ImVlETA/edit#gid=372848906

url = f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv&gid={tab_id}"
pd.read_csv(url)

Unnamed: 0Unnamed: 1Unnamed: 2Unnamed: 3Unnamed: 4Unnamed: 5Unnamed: 6Unnamed: 7Unnamed: 8Unnamed: 9...Unnamed: 17Unnamed: 18Unnamed: 19Unnamed: 20Unnamed: 21Unnamed: 22Unnamed: 23Unnamed: 24Unnamed: 25Unnamed: 26
0Belfast SouthNaNStage 1Surplus Hargey2.00Exclude <5003.00Exclude McCann Sibanda4.00Exclude Girvin...NaNNaNNaNNaNNaN11.0012.013.014.015.0
1Deirdre HargeySinn Féin9511-16877824.00NaN7824.00NaN7824.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
2Edwin PootsDemocratic Unionist Party72116.127217.126.087223.20117.727340.921134...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
3Matthew O'TooleSocial Democratic and Labour Party5394664.926058.9268.186127.10322.046449.1410.18...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4Paula BradshawAlliance Party6503265.146768.1459.56827.64135.826963.4631...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
5Kate NichollAlliance Party5201145.985346.9857.525404.50131.285535.7817...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
6Clare BaileyGreen Party4058167.44225.40169.824395.22490.544885.7648.36...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
7Stephen McCarthyUlster Unionist Party30616.123067.1217.723084.8425.183110.02643.36...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
8Elsie TrainorSocial Democratic and Labour Party2030181.82211.8018.522230.32132.942363.264...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
9Andrew GirvinTraditional Unionist Voice19350.361935.3691944.3635.541979.90-1979.9...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
10Luke McCannAontú80670.92876.9215.16892.08-892.080.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
11Sipho SibandaPeople Before Profit62940.5669.50172.66842.16-842.160.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
12Neil MooreSocialist Party35318371.00-3710.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
13Paddy LynnThe Workers Party13924.48163.48-163.480.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
14Elly OdhiamboIndependent1076.84113.84-113.840.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
15ExhaustedNaNNaN88.4288.4254.16142.58343.18485.7692...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
16NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
17Eligible Voters73497NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
18Turnout47306NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
19% Turnout64.36%NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
20Valid Ballots46938NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
21invalid ballots368NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
22quota7824NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
23NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
24NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
25Belfast WestNaNStage 1Surplus Baker2.00Exclude Hill Mallon3.00Exclude Crossan4.00Exclude Burns...Exclude Doran9.00Exclude Murphy Higgins10.00Exclude Doherty11.00NaNNaNNaNNaN
26Danny BakerSinn Féin9011-17337278.00NaN7278.00NaN7278.00NaN...NaN7278.00NaN7278.00NaN7278.00NaNNaNNaNNaN
27Órlaithí FlynnSinn Féin6743344.477087.475.387092.8516.097108.9427.37...68.377228.631797407.63NaN7407.63NaNNaNNaNNaN
28Aisling ReillySinn Féin56811028.476709.4710.196719.667.766727.4217.09...50.526811.98318.987130.965337663.96NaNNaNNaNNaN
29Pat SheehanSinn Féin637052.446422.4446426.4456431.448.38...26.716477.10258.186735.28451.137186.41NaNNaNNaNNaN
30Gerry CarrollPeople Before Profit3279115.333394.3319.763414.0978.383492.4770.08...238.233936.16542.994479.151543.466022.61NaNNaNNaNNaN
31Frank McCoubreyDemocratic Unionist Party41660.574166.577.194173.7604173.762...76.195275.141545429.1460.575489.71NaNNaNNaNNaN
32Paul DohertySocial Democratic and Labour Party252888.352616.352.192618.5429.142647.6836.28...478.513232.66404.283636.94-3636.940.00NaNNaNNaNNaN
33Gerard HerdmanAontú17538.171761.17321793.179.191802.3617.19...35.191871.74-1871.740.00NaNNaNNaNNaNNaNNaN
34Dan MurphyIrish Republican Socialist Party110312.161115.1681123.1671130.1614...71159.16-1159.160.00NaNNaNNaNNaNNaNNaN
35Donnamarie HigginsAlliance Party90717.48924.482926.486932.487.19...-1134.810.00NaNNaNNaNNaNNaNNaNNaNNaN
36Jordan DoranTraditional Unionist Voice8020.38802.383805.381806.383...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
37Linsey GibsonUlster Unionist Party4740.76474.762476.761.19477.952...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
38Stevie MaginnGreen Party3073.04310.043313.0410323.0410.19...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
39Gerard BurnsIndependent19216.91208.9128.19237.107244.10-244.1...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
40Patrick CrossanThe Workers Party1934.75197.756.76204.51-204.510.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
41Tony MallonIndependent1292.28131.28-131.280.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
42Declan HillIndependent260.3826.38-26.380.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
43ExhaustedNaNNaN37.0637.062461.0626.7687.8229.33...154.09393.431173.471566.901048.782615.68NaNNaNNaNNaN
44NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
45Eligible Voters68727NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
46Turnout44440NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
47% Turnout64.66%NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
48Valid Ballots43664NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
49invalid ballots776NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
50quota7278NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN

51 rows × 27 columns

This is a little bit more complicated than other google foo would have you believe but it looks like Google updated their API’s over the years to remove the ‘happy path’ for this call.

Additionally, note that as in the IMPORTRANGE example, there are no usable ‘headers’ in the underlying data so we may have to create these ourselves for more complex analysis.

Also, we have to manually ’tidy up’ the ‘range’ ourselves, as the Belfast South range only goes to row 24, and then Belfast West appears.

While we could get fancy, for simplicity, this is a manual example. And we’ll also exclude the ‘metadata’ such as the Turnout statistics and Quota from the bottom of the section, so in this case trimming the data from to run between rows 3 and 17 for just candidates and transfer statistics.

df = pd.read_csv(url)
df

Unnamed: 0Unnamed: 1Unnamed: 2Unnamed: 3Unnamed: 4Unnamed: 5Unnamed: 6Unnamed: 7Unnamed: 8Unnamed: 9...Unnamed: 17Unnamed: 18Unnamed: 19Unnamed: 20Unnamed: 21Unnamed: 22Unnamed: 23Unnamed: 24Unnamed: 25Unnamed: 26
0Belfast SouthNaNStage 1Surplus Hargey2.00Exclude <5003.00Exclude McCann Sibanda4.00Exclude Girvin...NaNNaNNaNNaNNaN11.0012.013.014.015.0
1Deirdre HargeySinn Féin9511-16877824.00NaN7824.00NaN7824.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
2Edwin PootsDemocratic Unionist Party72116.127217.126.087223.20117.727340.921134...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
3Matthew O'TooleSocial Democratic and Labour Party5394664.926058.9268.186127.10322.046449.1410.18...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4Paula BradshawAlliance Party6503265.146768.1459.56827.64135.826963.4631...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
5Kate NichollAlliance Party5201145.985346.9857.525404.50131.285535.7817...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
6Clare BaileyGreen Party4058167.44225.40169.824395.22490.544885.7648.36...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
7Stephen McCarthyUlster Unionist Party30616.123067.1217.723084.8425.183110.02643.36...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
8Elsie TrainorSocial Democratic and Labour Party2030181.82211.8018.522230.32132.942363.264...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
9Andrew GirvinTraditional Unionist Voice19350.361935.3691944.3635.541979.90-1979.9...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
10Luke McCannAontú80670.92876.9215.16892.08-892.080.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
11Sipho SibandaPeople Before Profit62940.5669.50172.66842.16-842.160.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
12Neil MooreSocialist Party35318371.00-3710.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
13Paddy LynnThe Workers Party13924.48163.48-163.480.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
14Elly OdhiamboIndependent1076.84113.84-113.840.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
15ExhaustedNaNNaN88.4288.4254.16142.58343.18485.7692...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
16NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
17Eligible Voters73497NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
18Turnout47306NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
19% Turnout64.36%NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
20Valid Ballots46938NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
21invalid ballots368NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
22quota7824NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
23NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
24NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
25Belfast WestNaNStage 1Surplus Baker2.00Exclude Hill Mallon3.00Exclude Crossan4.00Exclude Burns...Exclude Doran9.00Exclude Murphy Higgins10.00Exclude Doherty11.00NaNNaNNaNNaN
26Danny BakerSinn Féin9011-17337278.00NaN7278.00NaN7278.00NaN...NaN7278.00NaN7278.00NaN7278.00NaNNaNNaNNaN
27Órlaithí FlynnSinn Féin6743344.477087.475.387092.8516.097108.9427.37...68.377228.631797407.63NaN7407.63NaNNaNNaNNaN
28Aisling ReillySinn Féin56811028.476709.4710.196719.667.766727.4217.09...50.526811.98318.987130.965337663.96NaNNaNNaNNaN
29Pat SheehanSinn Féin637052.446422.4446426.4456431.448.38...26.716477.10258.186735.28451.137186.41NaNNaNNaNNaN
30Gerry CarrollPeople Before Profit3279115.333394.3319.763414.0978.383492.4770.08...238.233936.16542.994479.151543.466022.61NaNNaNNaNNaN
31Frank McCoubreyDemocratic Unionist Party41660.574166.577.194173.7604173.762...76.195275.141545429.1460.575489.71NaNNaNNaNNaN
32Paul DohertySocial Democratic and Labour Party252888.352616.352.192618.5429.142647.6836.28...478.513232.66404.283636.94-3636.940.00NaNNaNNaNNaN
33Gerard HerdmanAontú17538.171761.17321793.179.191802.3617.19...35.191871.74-1871.740.00NaNNaNNaNNaNNaNNaN
34Dan MurphyIrish Republican Socialist Party110312.161115.1681123.1671130.1614...71159.16-1159.160.00NaNNaNNaNNaNNaNNaN
35Donnamarie HigginsAlliance Party90717.48924.482926.486932.487.19...-1134.810.00NaNNaNNaNNaNNaNNaNNaNNaN
36Jordan DoranTraditional Unionist Voice8020.38802.383805.381806.383...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
37Linsey GibsonUlster Unionist Party4740.76474.762476.761.19477.952...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
38Stevie MaginnGreen Party3073.04310.043313.0410323.0410.19...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
39Gerard BurnsIndependent19216.91208.9128.19237.107244.10-244.1...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
40Patrick CrossanThe Workers Party1934.75197.756.76204.51-204.510.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
41Tony MallonIndependent1292.28131.28-131.280.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
42Declan HillIndependent260.3826.38-26.380.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
43ExhaustedNaNNaN37.0637.062461.0626.7687.8229.33...154.09393.431173.471566.901048.782615.68NaNNaNNaNNaN
44NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
45Eligible Voters68727NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
46Turnout44440NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
47% Turnout64.66%NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
48Valid Ballots43664NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
49invalid ballots776NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
50quota7278NaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN

51 rows × 27 columns

df.iloc[1:16]

Unnamed: 0Unnamed: 1Unnamed: 2Unnamed: 3Unnamed: 4Unnamed: 5Unnamed: 6Unnamed: 7Unnamed: 8Unnamed: 9...Unnamed: 17Unnamed: 18Unnamed: 19Unnamed: 20Unnamed: 21Unnamed: 22Unnamed: 23Unnamed: 24Unnamed: 25Unnamed: 26
1Deirdre HargeySinn Féin9511-16877824.00NaN7824.00NaN7824.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
2Edwin PootsDemocratic Unionist Party72116.127217.126.087223.20117.727340.921134...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
3Matthew O'TooleSocial Democratic and Labour Party5394664.926058.9268.186127.10322.046449.1410.18...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4Paula BradshawAlliance Party6503265.146768.1459.56827.64135.826963.4631...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
5Kate NichollAlliance Party5201145.985346.9857.525404.50131.285535.7817...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
6Clare BaileyGreen Party4058167.44225.40169.824395.22490.544885.7648.36...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
7Stephen McCarthyUlster Unionist Party30616.123067.1217.723084.8425.183110.02643.36...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
8Elsie TrainorSocial Democratic and Labour Party2030181.82211.8018.522230.32132.942363.264...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
9Andrew GirvinTraditional Unionist Voice19350.361935.3691944.3635.541979.90-1979.9...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
10Luke McCannAontú80670.92876.9215.16892.08-892.080.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
11Sipho SibandaPeople Before Profit62940.5669.50172.66842.16-842.160.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
12Neil MooreSocialist Party35318371.00-3710.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
13Paddy LynnThe Workers Party13924.48163.48-163.480.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
14Elly OdhiamboIndependent1076.84113.84-113.840.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
15ExhaustedNaNNaN88.4288.4254.16142.58343.18485.7692...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN

15 rows × 27 columns

There are a few ways to tidy up this stage/transfer setup, so for simplicity we’ll take the ‘index’ off the left of the table (consisting of the candidate and party names) and try and construct a new column index based on those.

Sounds fancy.

df.iloc[1:16]

Unnamed: 0Unnamed: 1Unnamed: 2Unnamed: 3Unnamed: 4Unnamed: 5Unnamed: 6Unnamed: 7Unnamed: 8Unnamed: 9...Unnamed: 17Unnamed: 18Unnamed: 19Unnamed: 20Unnamed: 21Unnamed: 22Unnamed: 23Unnamed: 24Unnamed: 25Unnamed: 26
1Deirdre HargeySinn Féin9511-16877824.00NaN7824.00NaN7824.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
2Edwin PootsDemocratic Unionist Party72116.127217.126.087223.20117.727340.921134...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
3Matthew O'TooleSocial Democratic and Labour Party5394664.926058.9268.186127.10322.046449.1410.18...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4Paula BradshawAlliance Party6503265.146768.1459.56827.64135.826963.4631...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
5Kate NichollAlliance Party5201145.985346.9857.525404.50131.285535.7817...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
6Clare BaileyGreen Party4058167.44225.40169.824395.22490.544885.7648.36...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
7Stephen McCarthyUlster Unionist Party30616.123067.1217.723084.8425.183110.02643.36...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
8Elsie TrainorSocial Democratic and Labour Party2030181.82211.8018.522230.32132.942363.264...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
9Andrew GirvinTraditional Unionist Voice19350.361935.3691944.3635.541979.90-1979.9...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
10Luke McCannAontú80670.92876.9215.16892.08-892.080.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
11Sipho SibandaPeople Before Profit62940.5669.50172.66842.16-842.160.00NaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
12Neil MooreSocialist Party35318371.00-3710.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
13Paddy LynnThe Workers Party13924.48163.48-163.480.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
14Elly OdhiamboIndependent1076.84113.84-113.840.00NaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
15ExhaustedNaNNaN88.4288.4254.16142.58343.18485.7692...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN

15 rows × 27 columns

_table = df.iloc[1:16]
_table = _table.dropna(how='all', axis=1)
_table = _table.set_index(_table.columns.tolist()[0:2])
_table = _table.fillna(0)
_table = _table.astype(float)
_table.index=_table.index.set_names(['Candidate','Party'])
_table

Unnamed: 2Unnamed: 3Unnamed: 4Unnamed: 5Unnamed: 6Unnamed: 7Unnamed: 8Unnamed: 9Unnamed: 10Unnamed: 11Unnamed: 12Unnamed: 13Unnamed: 14Unnamed: 15Unnamed: 16
CandidateParty
Deirdre HargeySinn Féin9511.0-1687.007824.000.007824.000.007824.000.007824.000.007824.000.007824.000.007824.00
Edwin PootsDemocratic Unionist Party7211.06.127217.126.087223.20117.727340.921134.008474.920.008474.920.008474.92-650.927824.00
Matthew O'TooleSocial Democratic and Labour Party5394.0664.926058.9268.186127.10322.046449.1410.186459.321630.008089.320.008089.320.008089.32
Paula BradshawAlliance Party6503.0265.146768.1459.506827.64135.826963.4631.006994.46214.687209.141114.008323.140.008323.14
Kate NichollAlliance Party5201.0145.985346.9857.525404.50131.285535.7817.005552.78244.905797.68858.426656.1081.006737.10
Clare BaileyGreen Party4058.0167.404225.40169.824395.22490.544885.7648.364934.12164.025098.14600.965699.10127.005826.10
Stephen McCarthyUlster Unionist Party3061.06.123067.1217.723084.8425.183110.02643.363753.3816.903770.28-3770.280.000.000.00
Elsie TrainorSocial Democratic and Labour Party2030.0181.802211.8018.522230.32132.942363.264.002367.26-2367.260.000.000.000.000.00
Andrew GirvinTraditional Unionist Voice1935.00.361935.369.001944.3635.541979.90-1979.900.000.000.000.000.000.000.00
Luke McCannAontú806.070.92876.9215.16892.08-892.080.000.000.000.000.000.000.000.000.00
Sipho SibandaPeople Before Profit629.040.50669.50172.66842.16-842.160.000.000.000.000.000.000.000.000.00
Neil MooreSocialist Party353.018.00371.00-371.000.000.000.000.000.000.000.000.000.000.000.00
Paddy LynnThe Workers Party139.024.48163.48-163.480.000.000.000.000.000.000.000.000.000.000.00
Elly OdhiamboIndependent107.06.84113.84-113.840.000.000.000.000.000.000.000.000.000.000.00
ExhaustedNaN0.088.4288.4254.16142.58343.18485.7692.00577.7696.76674.521196.901871.42442.922314.34

Now to do the same thing with the stage counts and transfers;

There are many ways to do this, either leaving it as is and just naming the columns by Stage and Transfer, but a ‘better’ way to do it is to create a multiindex on the column. Which probably means nothing.

_table.columns
Index(['Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4', 'Unnamed: 5', 'Unnamed: 6',
       'Unnamed: 7', 'Unnamed: 8', 'Unnamed: 9', 'Unnamed: 10', 'Unnamed: 11',
       'Unnamed: 12', 'Unnamed: 13', 'Unnamed: 14', 'Unnamed: 15',
       'Unnamed: 16'],
      dtype='object')
stage = ['Count','Transfers']
stages = range(1,_table.shape[1]//2+2)
pd.MultiIndex.from_product([stages,stage])[:-1]
MultiIndex([(1,     'Count'),
            (1, 'Transfers'),
            (2,     'Count'),
            (2, 'Transfers'),
            (3,     'Count'),
            (3, 'Transfers'),
            (4,     'Count'),
            (4, 'Transfers'),
            (5,     'Count'),
            (5, 'Transfers'),
            (6,     'Count'),
            (6, 'Transfers'),
            (7,     'Count'),
            (7, 'Transfers'),
            (8,     'Count')],
           )
_table.columns=pd.MultiIndex.from_product([stages,stage], names=['Stage','Step'])[:-1]
_table

Stage12345678
StepCountTransfersCountTransfersCountTransfersCountTransfersCountTransfersCountTransfersCountTransfersCount
CandidateParty
Deirdre HargeySinn Féin9511.0-1687.007824.000.007824.000.007824.000.007824.000.007824.000.007824.000.007824.00
Edwin PootsDemocratic Unionist Party7211.06.127217.126.087223.20117.727340.921134.008474.920.008474.920.008474.92-650.927824.00
Matthew O'TooleSocial Democratic and Labour Party5394.0664.926058.9268.186127.10322.046449.1410.186459.321630.008089.320.008089.320.008089.32
Paula BradshawAlliance Party6503.0265.146768.1459.506827.64135.826963.4631.006994.46214.687209.141114.008323.140.008323.14
Kate NichollAlliance Party5201.0145.985346.9857.525404.50131.285535.7817.005552.78244.905797.68858.426656.1081.006737.10
Clare BaileyGreen Party4058.0167.404225.40169.824395.22490.544885.7648.364934.12164.025098.14600.965699.10127.005826.10
Stephen McCarthyUlster Unionist Party3061.06.123067.1217.723084.8425.183110.02643.363753.3816.903770.28-3770.280.000.000.00
Elsie TrainorSocial Democratic and Labour Party2030.0181.802211.8018.522230.32132.942363.264.002367.26-2367.260.000.000.000.000.00
Andrew GirvinTraditional Unionist Voice1935.00.361935.369.001944.3635.541979.90-1979.900.000.000.000.000.000.000.00
Luke McCannAontú806.070.92876.9215.16892.08-892.080.000.000.000.000.000.000.000.000.00
Sipho SibandaPeople Before Profit629.040.50669.50172.66842.16-842.160.000.000.000.000.000.000.000.000.00
Neil MooreSocialist Party353.018.00371.00-371.000.000.000.000.000.000.000.000.000.000.000.00
Paddy LynnThe Workers Party139.024.48163.48-163.480.000.000.000.000.000.000.000.000.000.000.00
Elly OdhiamboIndependent107.06.84113.84-113.840.000.000.000.000.000.000.000.000.000.000.00
ExhaustedNaN0.088.4288.4254.16142.58343.18485.7692.00577.7696.76674.521196.901871.42442.922314.34

Now we can do some interesting queries really easily;

_table.xs("Count", level='Step', axis=1)

Stage12345678
CandidateParty
Deirdre HargeySinn Féin9511.07824.007824.007824.007824.007824.007824.007824.00
Edwin PootsDemocratic Unionist Party7211.07217.127223.207340.928474.928474.928474.927824.00
Matthew O'TooleSocial Democratic and Labour Party5394.06058.926127.106449.146459.328089.328089.328089.32
Paula BradshawAlliance Party6503.06768.146827.646963.466994.467209.148323.148323.14
Kate NichollAlliance Party5201.05346.985404.505535.785552.785797.686656.106737.10
Clare BaileyGreen Party4058.04225.404395.224885.764934.125098.145699.105826.10
Stephen McCarthyUlster Unionist Party3061.03067.123084.843110.023753.383770.280.000.00
Elsie TrainorSocial Democratic and Labour Party2030.02211.802230.322363.262367.260.000.000.00
Andrew GirvinTraditional Unionist Voice1935.01935.361944.361979.900.000.000.000.00
Luke McCannAontú806.0876.92892.080.000.000.000.000.00
Sipho SibandaPeople Before Profit629.0669.50842.160.000.000.000.000.00
Neil MooreSocialist Party353.0371.000.000.000.000.000.000.00
Paddy LynnThe Workers Party139.0163.480.000.000.000.000.000.00
Elly OdhiamboIndependent107.0113.840.000.000.000.000.000.00
ExhaustedNaN0.088.42142.58485.76577.76674.521871.422314.34

This makes plotting quite simple; which then makes the queries you can express much more complex…

_table.xs("Count", level='Step', axis=1).groupby('Party').sum().T.plot()
<Axes: xlabel='Stage'>

png

_table.xs("Transfers", level='Step', axis=1)\
    .groupby('Party').sum().cumsum().T.plot(
        title='Net Cumulative Transfers'
    )\
    .legend(loc='right', bbox_to_anchor=(1.6,0.5))
<matplotlib.legend.Legend at 0x17b555a50>

png

Conclusion

Considering it’s election day today in Northern Ireland, I wanted to bash this out to help other election observers have a play with the fantastic work the likes of @colm_burns and the rest of the @electionsni team are doing.

More than happy to help anyone else answer interesting electoral questions, and remember; vote early, vote often, and #votetillyouboke