In [2]:
# From https://github.com/davidbjourno/finding-stories-in-data/blob/master/finding-stories-in-data.ipynb
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
%matplotlib inline
In [3]:
# Pass in the URL of the CSV file:
df = pd.read_csv('https://raw.githubusercontent.com/davidbjourno/finding-stories-in-data/master/data/leave-demographics.csv')
In [7]:
df.head(n=10)
Out[7]:
ons_id
name
region_name
electorate
result
leave
turnout
votes_cast
var1
var2
var3
var4
var5
var6
var7
var8
var9
0
E06000001
Hartlepool
North East
70341
leave
69.6
65.6
46134
48.8
23.9
49.5
15.9
17.6
495.2
30.5
25.6
13.0
1
E06000002
Middlesbrough
North East
94612
leave
65.5
64.9
61393
48.5
25.0
66.1
42.6
18.5
467.6
64.9
22.6
13.3
2
E06000003
Redcar and Cleveland
North East
103529
leave
66.2
70.3
72741
16.8
22.3
25.0
64.0
18.9
505.5
18.6
23.8
13.2
3
E06000004
Stockton-on-Tees
North East
141486
leave
61.7
71.0
100460
24.4
25.2
58.3
17.2
22.8
502.5
45.3
20.5
15.9
4
E06000005
Darlington
North East
77662
leave
56.2
71.1
55195
23.8
24.6
55.6
50.5
23.7
484.0
29.9
21.7
15.2
5
E06000006
Halton
North West
95289
leave
57.4
68.3
65047
19.9
25.4
30.2
47.7
17.6
484.7
46.1
21.0
11.9
6
E06000007
Warrington
North West
157042
leave
54.3
73.4
115205
4.6
26.0
35.4
27.5
27.4
545.6
33.7
15.5
18.1
7
E06000008
Blackburn with Darwen
North West
100117
leave
56.3
65.3
65408
21.2
26.8
6.5
31.8
19.8
413.0
51.6
18.2
14.0
8
E06000009
Blackpool
North West
102354
leave
67.5
65.4
66959
43.9
23.3
45.9
15.2
15.5
392.4
46.6
24.4
10.3
9
E06000010
Hull
Yorkshire and The Humber
180230
leave
67.6
62.9
113436
60.9
28.2
7.7
31.0
15.2
445.9
31.4
26.9
10.5
In [9]:
plt.figure(
figsize=(8,8),
dpi=72)
# Plot the data as a scatter plot
g = plt.scatter(
x=df['var1'], # The values we want to plot along the x axis
y=df['leave'], # The values we want to plot along the y axis
s=50, # The size…
c='#0571b0', # …colour…
alpha=0.5 # …and opacity we want the data point markers to be
)
In [11]:
plt.figure(
figsize=(8, 8),
dpi=72
)
# Plot the data as a scatter plot
g = plt.scatter(
x=df['var2'], # The values we want to plot along the x axis
y=df['leave'], # The values we want to plot along the y axis
s=50, # The size…
c='#0571b0', # …colour…
alpha=0.5 # …and opacity we want the data point markers to be
)
In [12]:
# Add london t/f to data frame
df['is_london'] = np.where(df['region_name'] == 'London', True, False)
# Print all the rows in the dataframe in which is_london is equal to True
df[df['is_london'] == True]
Out[12]:
ons_id
name
region_name
electorate
result
leave
turnout
votes_cast
var1
var2
var3
var4
var5
var6
var7
var8
var9
is_london
293
E09000001
City of London
London
5987
remain
24.7
73.6
4405
65.7
37.2
37.4
25.2
68.4
864.7
27.6
3.2
39.9
True
294
E09000002
Barking and Dagenham
London
115812
leave
62.4
63.9
73941
38.6
31.2
34.5
18.2
20.9
534.5
43.2
16.0
13.3
True
295
E09000003
Barnet
London
223467
remain
37.8
72.1
161218
20.3
31.9
49.7
30.1
40.3
629.8
66.4
5.6
24.7
True
296
E09000004
Bexley
London
170779
leave
63.0
75.3
128570
5.5
26.5
29.8
56.1
21.8
612.7
31.6
12.3
14.7
True
297
E09000005
Brent
London
186793
remain
40.3
65.1
121671
35.8
34.3
50.3
12.9
33.3
551.4
30.8
5.1
18.2
True
298
E09000006
Bromley
London
231473
remain
49.4
78.9
182570
11.5
27.5
42.1
62.2
33.1
681.0
65.0
9.6
21.4
True
299
E09000007
Camden
London
145425
remain
25.1
65.5
95281
29.0
37.5
44.9
5.3
50.5
719.8
31.6
4.8
31.9
True
300
E09000008
Croydon
London
245349
remain
45.7
69.8
171289
13.8
29.7
37.7
12.2
31.8
602.8
12.4
10.4
19.6
True
301
E09000009
Ealing
London
212991
remain
39.6
70.1
149268
30.8
33.9
66.4
18.2
37.0
562.2
34.4
6.2
21.0
True
302
E09000010
Enfield
London
198387
remain
44.2
69.1
137056
14.5
29.9
47.3
68.2
28.7
564.0
10.6
8.6
19.0
True
303
E09000011
Greenwich
London
168967
remain
44.4
69.5
117470
60.6
35.0
23.2
56.6
33.2
601.0
62.0
12.1
20.4
True
304
E09000012
Hackney
London
163284
remain
21.5
65.2
106422
61.6
43.7
66.7
56.8
41.8
603.4
20.1
6.8
25.0
True
305
E09000013
Hammersmith and Fulham
London
114863
remain
30.0
70.0
80347
48.9
40.6
59.9
53.6
49.6
686.1
23.6
5.5
27.0
True
306
E09000014
Haringey
London
150098
remain
24.4
70.6
106032
30.1
38.6
18.2
36.8
40.8
576.6
17.2
6.2
23.9
True
307
E09000015
Harrow
London
162397
remain
45.4
72.3
117352
5.5
30.4
41.2
51.5
36.8
625.7
32.5
5.3
23.2
True
308
E09000016
Havering
London
183082
leave
69.7
76.0
139175
61.2
26.1
3.5
21.8
19.4
626.3
22.5
12.3
14.1
True
309
E09000017
Hillingdon
London
193033
leave
56.4
69.0
133170
27.6
30.6
46.9
20.7
28.0
605.5
43.6
9.5
17.1
True
310
E09000018
Hounslow
London
165050
remain
48.9
69.8
115208
40.9
35.1
13.7
40.6
34.6
565.7
24.9
8.2
18.5
True
311
E09000019
Islington
London
144514
remain
24.8
70.4
101723
52.1
41.7
9.6
56.8
48.1
670.8
37.2
7.1
31.3
True
312
E09000020
Kensington and Chelsea
London
83042
remain
31.3
66.0
54801
45.8
35.5
5.1
43.0
52.7
762.8
11.7
3.2
23.4
True
313
E09000021
Kingston-upon-Thames
London
108838
remain
38.4
78.4
85330
32.1
32.4
44.8
67.8
41.4
701.9
39.6
7.5
25.2
True
314
E09000022
Lambeth
London
210800
remain
21.4
67.4
142162
53.8
44.2
36.9
19.5
46.6
621.5
65.4
7.1
25.7
True
315
E09000023
Lewisham
London
197514
remain
30.1
63.1
124637
61.1
37.4
12.8
31.5
38.0
606.4
39.1
10.2
22.6
True
316
E09000024
Merton
London
136352
remain
37.1
73.5
100207
59.8
35.5
32.3
42.4
41.1
622.4
66.6
7.8
24.4
True
317
E09000025
Newham
London
176985
remain
47.2
59.3
104864
49.3
38.6
14.1
17.1
30.1
505.5
67.9
7.2
14.8
True
318
E09000026
Redbridge
London
189843
remain
46.0
67.6
128397
10.9
32.2
58.8
53.0
33.9
652.5
16.9
6.5
22.9
True
319
E09000027
Richmond-upon-Thames
London
132632
remain
30.7
82.1
108876
4.8
30.9
27.6
56.4
53.0
744.2
53.6
5.9
29.7
True
320
E09000028
Southwark
London
195875
remain
27.2
66.2
129677
34.1
40.3
9.3
24.7
43.1
644.2
23.0
8.0
25.8
True
321
E09000029
Sutton
London
140288
leave
53.7
76.0
106633
23.6
29.6
6.3
4.2
30.1
605.9
45.9
10.0
19.2
True
322
E09000030
Tower Hamlets
London
167820
remain
32.5
64.6
108420
44.6
45.3
30.3
18.3
41.0
637.0
16.9
6.6
25.7
True
323
E09000031
Waltham Forest
London
162983
remain
40.9
66.7
108689
56.9
35.5
49.0
65.6
30.0
546.1
53.6
8.7
17.9
True
324
E09000032
Wandsworth
London
219521
remain
25.0
72.0
158018
67.1
45.1
16.9
34.2
53.6
718.5
10.6
6.2
29.0
True
325
E09000033
Westminster
London
120524
remain
31.0
65.0
78325
15.0
40.0
45.3
64.8
50.3
764.4
29.0
3.6
26.6
True
In [25]:
# From now on, using Seaborne instead of Matplotlib
# Set the chart background colour (not necessary)
sns.set_style('darkgrid', { 'axes.facecolor': '#efefef' })
# Tell Seaborn that what we want from it is a FacetGrid, and assign this to the variable ‘fg’
fg = sns.FacetGrid(
data=df, # Use our dataframe as the input data
hue='is_london', # Highlight the data points for which is_london == True
palette=['#0571b0', '#ca0020'], # Define a tasteful blue/red colour combo
size=7 # Make the plots size 7, whatever that means
)
# Tell Seaborn that what we want to do with our FacetGrid (fg) is visualise it as a scatter plot
fg.map(
plt.scatter,
'var2', # Values to plot along the x axis
'leave', # Values to plot along the y axis
alpha=0.5
)
Out[25]:
<seaborn.axisgrid.FacetGrid at 0x11166e518>
In [27]:
# Add data point True/False column to flag the areas with the largest populations—the ones with electorates of 100,000 people or more
df['is_largest'] = np.where(df['electorate'] > 100000, True, False)
g = sns.FacetGrid(
data=df,
hue='is_london', col='is_largest',
palette=['#0571b0', '#ca0020'],
size=7)
g.map(
plt.scatter,
'var2', # Values to plot along the x axis
'leave', # Values to plot along the y axis
alpha=0.5
)
Out[27]:
<seaborn.axisgrid.FacetGrid at 0x1119fb080>
In [28]:
# correlation diagram: plot each variable against every other variable listed
columns = ['var1', 'var2', 'var3', 'var4', 'leave', 'is_london']
g = sns.PairGrid(
data=df[columns],
hue='is_london',
palette=['#0571b0', '#ca0020']
)
g.map_offdiag(plt.scatter);
In [29]:
# correlation diagram: plot each variable against every other variable listed
columns = ['var1', 'var2', 'var3', 'var4', 'var5', 'var6', 'var7', 'var8', 'var9', 'leave', 'is_london']
g = sns.PairGrid(
data=df[columns],
hue='is_london',
palette=['#0571b0', '#ca0020']
)
g.map_offdiag(plt.scatter);
In [32]:
# Heatmap methods to compare the correlation coefficient
plt.figure(figsize=(15, 15), dpi=72)
columns = [ # ALL THE COLUMNS
'var1',
'var2',
'var3',
'var4',
'var5',
'var6',
'var7',
'var8',
'var9',
'leave'
]
correlations=df[columns].corr(method='pearson')
sns.heatmap(
data=correlations,
square=True,
xticklabels=correlations.columns.values,
yticklabels=correlations.columns.values,
# The Matplotlib colormap to use
# (https://matplotlib.org/examples/color/colormaps_reference.html)
cmap='plasma'
)
Out[32]:
<matplotlib.axes._subplots.AxesSubplot at 0x1180599e8>
In [45]:
# London boroughs returned higher or lower ‘leave’ vote percentages than we would expect
# based on the values of any correlating variable.
# A convenient way to do this would be to use Seaborn's built-in linear regression plotting
columns = ['var1', 'leave']
g = sns.lmplot(
data=df,
x=columns[0],
y=columns[1],
hue='is_london',
palette=['#0571b0', '#ca0020'],
size=7,
fit_reg=False,
)
sns.regplot(
data=df,
x=columns[0],
y=columns[1],
scatter=False,
color='#0571b0',
ax=g.axes[0, 0]
)
# Reading this plot, we're able to say that, all things being equal,
# most of the London boroughs have lower ‘leave’ vote percentages
# than we would expect based on their levels of var2 alone. This suggests—rightly—that
# variables other than var2 are in play in determining London's lower-than-expected levels of ‘leave’ voting.
Out[45]:
<matplotlib.axes._subplots.AxesSubplot at 0x1181c7d30>
In [47]:
# Configure graphic
color_map = {False: '#0571b0', True: '#ca0020'}
# Instantiate our plot
p = figure(
plot_width=600,
plot_height=422,
background_fill_color='#d3d3d3',
title='Leave demographics'
)
# Add a circle renderer to the plot
p.circle(
x=df['var5'],
y=df['leave'],
# Size the markers according to the size of the electorate (scaled down)
size=df['electorate'] / 20000,
fill_color=df['is_london'].map(color_map),
line_color=df['is_london'].map(color_map),
line_width=1,
alpha=0.5
)
# Configure the plot's x axis
p.xaxis.axis_label = 'var5'
p.xgrid.grid_line_color = None
# Configure the plot's y axis
p.yaxis.axis_label = 'Percentage voting leave'
p.ygrid.grid_line_color = '#999999'
p.ygrid.grid_line_alpha = 1
p.ygrid.grid_line_dash = [6, 4]
# Show the plot
show(p)
In [48]:
df.head()
Out[48]:
ons_id
name
region_name
electorate
result
leave
turnout
votes_cast
var1
var2
var3
var4
var5
var6
var7
var8
var9
is_london
is_largest
0
E06000001
Hartlepool
North East
70341
leave
69.6
65.6
46134
48.8
23.9
49.5
15.9
17.6
495.2
30.5
25.6
13.0
False
False
1
E06000002
Middlesbrough
North East
94612
leave
65.5
64.9
61393
48.5
25.0
66.1
42.6
18.5
467.6
64.9
22.6
13.3
False
False
2
E06000003
Redcar and Cleveland
North East
103529
leave
66.2
70.3
72741
16.8
22.3
25.0
64.0
18.9
505.5
18.6
23.8
13.2
False
True
3
E06000004
Stockton-on-Tees
North East
141486
leave
61.7
71.0
100460
24.4
25.2
58.3
17.2
22.8
502.5
45.3
20.5
15.9
False
True
4
E06000005
Darlington
North East
77662
leave
56.2
71.1
55195
23.8
24.6
55.6
50.5
23.7
484.0
29.9
21.7
15.2
False
False
In [56]:
df.sort_values(by='leave', ascending=False)
Out[56]:
ons_id
name
region_name
electorate
result
leave
turnout
votes_cast
var1
var2
var3
var4
var5
var6
var7
var8
var9
is_london
is_largest
164
E07000136
Boston
East Midlands
39363
leave
75.6
77.3
30416
14.6
25.6
37.8
16.6
15.1
411.2
18.9
27.2
10.0
False
False
168
E07000140
South Holland
East Midlands
65701
leave
73.6
75.4
49518
52.1
22.9
20.3
67.7
15.8
443.7
31.1
25.9
9.7
False
False
101
E07000069
Castle Point
East of England
68860
leave
72.7
75.4
51909
21.0
21.1
50.4
46.5
14.6
565.9
48.4
14.1
11.0
False
False
33
E06000034
Thurrock
East of England
109897
leave
72.3
72.8
79950
29.0
29.2
41.9
46.5
17.4
563.7
48.0
15.6
11.3
False
True
173
E07000145
Great Yarmouth
East of England
72634
leave
71.5
69.1
50158
67.2
22.3
7.8
25.8
14.2
448.8
13.2
28.9
10.1
False
False
62
E07000010
Fenland
East of England
71447
leave
71.4
73.7
52649
62.8
23.4
20.4
52.4
14.9
457.7
6.7
26.5
9.4
False
False
196
E07000174
Mansfield
East Midlands
77624
leave
70.9
72.6
56370
11.2
25.3
47.7
41.8
16.4
435.9
7.0
25.6
11.1
False
False
72
E07000033
Bolsover
East Midlands
58063
leave
70.8
72.3
41999
13.9
24.3
28.2
46.2
15.8
428.1
4.2
27.4
10.9
False
False
165
E07000137
East Lindsey
East Midlands
107009
leave
70.7
74.9
80178
25.1
18.5
32.7
44.2
18.1
462.1
53.2
28.8
11.6
False
True
11
E06000012
North East Lincolnshire
Yorkshire and The Humber
116302
leave
69.9
67.9
79011
7.9
24.3
4.3
33.7
16.2
448.7
20.8
26.1
10.4
False
True
192
E07000170
Ashfield
East Midlands
91916
leave
69.8
72.8
66946
68.4
25.0
15.9
64.9
15.1
448.9
62.4
26.7
10.6
False
False
308
E09000016
Havering
London
183082
leave
69.7
76.0
139175
61.2
26.1
3.5
21.8
19.4
626.3
22.5
12.3
14.1
True
True
0
E06000001
Hartlepool
North East
70341
leave
69.6
65.6
46134
48.8
23.9
49.5
15.9
17.6
495.2
30.5
25.6
13.0
False
False
108
E07000076
Tendring
East of England
111167
leave
69.5
74.4
82703
35.1
18.1
64.8
10.4
15.9
493.5
53.4
23.4
10.9
False
True
20
E06000021
Stoke-on-Trent
West Midlands
179010
leave
69.4
65.7
117674
47.6
26.4
54.2
46.4
15.5
433.1
18.0
27.3
10.5
False
True
273
E08000017
Doncaster
Yorkshire and The Humber
217432
leave
69.0
69.6
151246
15.2
25.4
14.5
5.7
17.0
465.5
44.1
23.2
11.3
False
True
209
E07000192
Cannock Chase
West Midlands
75010
leave
68.9
71.5
53607
51.5
25.5
15.4
49.3
17.2
490.7
21.1
24.1
11.1
False
False
98
E07000066
Basildon
East of England
132771
leave
68.6
73.9
98062
24.5
26.6
4.3
66.3
18.6
558.8
15.0
16.2
13.7
False
True
272
E08000016
Barnsley
Yorkshire and The Humber
175809
leave
68.3
70.0
122972
14.2
24.8
67.4
54.5
17.4
469.0
45.0
23.4
12.4
False
True
105
E07000073
Harlow
East of England
59124
leave
68.1
73.6
43489
59.6
28.0
39.9
16.5
17.6
490.7
67.3
17.5
12.4
False
False
274
E08000018
Rotherham
Yorkshire and The Humber
197623
leave
67.9
69.6
137470
39.5
24.3
55.2
40.7
17.4
481.3
51.1
22.6
12.5
False
True
285
E08000030
Walsall
West Midlands
194729
leave
67.9
69.7
135685
7.6
25.5
48.4
20.9
16.9
475.9
36.3
24.8
12.6
False
True
193
E07000171
Bassetlaw
East Midlands
85547
leave
67.8
74.8
64003
26.5
22.6
28.0
55.4
20.2
506.3
36.5
23.3
12.8
False
False
282
E08000027
Dudley
West Midlands
244516
leave
67.6
71.7
175333
19.0
24.5
24.7
45.6
19.2
499.4
43.2
23.8
14.0
False
True
9
E06000010
Hull
Yorkshire and The Humber
180230
leave
67.6
62.9
113436
60.9
28.2
7.7
31.0
15.2
445.9
31.4
26.9
10.5
False
True
8
E06000009
Blackpool
North West
102354
leave
67.5
65.4
66959
43.9
23.3
45.9
15.2
15.5
392.4
46.6
24.4
10.3
False
True
216
E07000199
Tamworth
West Midlands
56825
leave
67.5
74.2
42151
67.8
26.2
43.9
41.3
17.4
472.0
56.4
22.2
11.5
False
False
235
E07000218
North Warwickshire
West Midlands
49790
leave
66.9
76.3
37975
15.0
22.9
57.6
9.6
19.8
492.6
34.3
20.4
13.0
False
False
283
E08000028
Sandwell
West Midlands
221429
leave
66.7
66.6
147418
6.3
27.8
42.2
45.4
15.4
453.0
5.0
24.3
11.1
False
True
107
E07000075
Rochford
East of England
66589
leave
66.6
78.8
52479
29.6
22.0
34.8
55.8
20.0
577.8
11.2
13.2
14.6
False
False
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
295
E09000003
Barnet
London
223467
remain
37.8
72.1
161218
20.3
31.9
49.7
30.1
40.3
629.8
66.4
5.6
24.7
True
True
253
E07000240
St Albans
East of England
104859
remain
37.3
82.5
86524
36.4
27.4
24.4
44.1
46.3
700.6
34.7
8.4
28.6
False
True
316
E09000024
Merton
London
136352
remain
37.1
73.5
100207
59.8
35.5
32.3
42.4
41.1
622.4
66.6
7.8
24.4
True
True
345
S12000029
South Lanarkshire
Scotland
248949
remain
36.9
65.4
162683
33.3
24.3
61.0
38.2
22.0
531.1
29.8
NaN
15.6
False
True
340
S12000023
Orkney Islands
Scotland
16658
remain
36.8
68.5
11402
12.4
21.8
36.8
56.4
26.5
505.2
55.3
NaN
12.7
False
False
336
S12000018
Inverclyde
Scotland
58624
remain
36.2
66.1
38722
26.2
22.8
45.5
57.3
19.7
518.9
48.5
NaN
15.0
False
False
330
S12000010
East Lothian
Scotland
77788
remain
35.4
71.7
55800
13.2
22.8
5.8
13.3
27.0
558.1
45.7
NaN
16.3
False
False
351
S12000038
Renfrewshire
Scotland
127294
remain
35.2
69.3
88197
10.2
24.5
44.7
53.7
23.1
534.0
25.3
NaN
15.7
False
True
358
S12000046
Glasgow
Scotland
449806
remain
33.4
56.3
253000
63.0
31.9
4.8
51.8
25.9
515.8
12.3
NaN
18.5
False
True
322
E09000030
Tower Hamlets
London
167820
remain
32.5
64.6
108420
44.6
45.3
30.3
18.3
41.0
637.0
16.9
6.6
25.7
True
True
346
S12000030
Stirling
Scotland
66100
remain
32.3
74.0
48931
24.4
23.1
23.0
61.3
33.3
511.5
5.4
NaN
20.0
False
False
42
E06000043
Brighton and Hove
South East
198293
remain
31.4
74.1
146829
20.6
31.2
5.2
49.9
36.9
546.6
46.6
10.7
22.7
False
True
312
E09000020
Kensington and Chelsea
London
83042
remain
31.3
66.0
54801
45.8
35.5
5.1
43.0
52.7
762.8
11.7
3.2
23.4
True
False
325
E09000033
Westminster
London
120524
remain
31.0
65.0
78325
15.0
40.0
45.3
64.8
50.3
764.4
29.0
3.6
26.6
True
True
319
E09000027
Richmond-upon-Thames
London
132632
remain
30.7
82.1
108876
4.8
30.9
27.6
56.4
53.0
744.2
53.6
5.9
29.7
True
True
315
E09000023
Lewisham
London
197514
remain
30.1
63.1
124637
61.1
37.4
12.8
31.5
38.0
606.4
39.1
10.2
22.6
True
True
305
E09000013
Hammersmith and Fulham
London
114863
remain
30.0
70.0
80347
48.9
40.6
59.9
53.6
49.6
686.1
23.6
5.5
27.0
True
True
200
E07000178
Oxford
South East
97331
remain
29.7
72.3
70411
15.0
30.5
59.2
15.8
42.6
559.0
55.7
11.3
33.6
False
False
357
S12000045
East Dunbartonshire
Scotland
83031
remain
28.6
75.2
62418
47.3
20.8
31.1
19.4
34.8
617.5
10.6
NaN
24.6
False
False
320
E09000028
Southwark
London
195875
remain
27.2
66.2
129677
34.1
40.3
9.3
24.7
43.1
644.2
23.0
8.0
25.8
True
True
60
E07000008
Cambridge
East of England
80108
remain
26.2
72.2
57852
50.8
31.6
58.6
50.8
47.3
583.3
34.9
11.0
38.0
False
False
331
S12000011
East Renfrewshire
Scotland
69575
remain
25.7
76.1
52969
9.9
21.2
68.3
12.2
35.2
668.6
12.8
NaN
25.0
False
False
350
S12000036
Edinburgh
Scotland
346073
remain
25.6
73.0
252481
29.2
32.8
20.4
20.7
41.4
554.2
14.3
NaN
25.6
False
True
299
E09000007
Camden
London
145425
remain
25.1
65.5
95281
29.0
37.5
44.9
5.3
50.5
719.8
31.6
4.8
31.9
True
True
324
E09000032
Wandsworth
London
219521
remain
25.0
72.0
158018
67.1
45.1
16.9
34.2
53.6
718.5
10.6
6.2
29.0
True
True
311
E09000019
Islington
London
144514
remain
24.8
70.4
101723
52.1
41.7
9.6
56.8
48.1
670.8
37.2
7.1
31.3
True
True
293
E09000001
City of London
London
5987
remain
24.7
73.6
4405
65.7
37.2
37.4
25.2
68.4
864.7
27.6
3.2
39.9
True
False
306
E09000014
Haringey
London
150098
remain
24.4
70.6
106032
30.1
38.6
18.2
36.8
40.8
576.6
17.2
6.2
23.9
True
True
304
E09000012
Hackney
London
163284
remain
21.5
65.2
106422
61.6
43.7
66.7
56.8
41.8
603.4
20.1
6.8
25.0
True
True
314
E09000022
Lambeth
London
210800
remain
21.4
67.4
142162
53.8
44.2
36.9
19.5
46.6
621.5
65.4
7.1
25.7
True
True
381 rows × 19 columns
In [66]:
df['number_of_leavers'] = df['votes_cast'] * df['leave'] / 100
df['leavers_per_capita'] = df['number_of_leavers'] / df['electorate']
df.sort_values(by='leavers_per_capita', ascending=True)
Out[66]:
ons_id
name
region_name
electorate
result
leave
turnout
votes_cast
var1
var2
...
var4
var5
var6
var7
var8
var9
is_london
is_largest
number_of_leavers
leavers_per_capita
304
E09000012
Hackney
London
163284
remain
21.5
65.2
106422
61.6
43.7
...
56.8
41.8
603.4
20.1
6.8
25.0
True
True
22880.730
0.140128
314
E09000022
Lambeth
London
210800
remain
21.4
67.4
142162
53.8
44.2
...
19.5
46.6
621.5
65.4
7.1
25.7
True
True
30422.668
0.144320
299
E09000007
Camden
London
145425
remain
25.1
65.5
95281
29.0
37.5
...
5.3
50.5
719.8
31.6
4.8
31.9
True
True
23915.531
0.164453
306
E09000014
Haringey
London
150098
remain
24.4
70.6
106032
30.1
38.6
...
36.8
40.8
576.6
17.2
6.2
23.9
True
True
25871.808
0.172366
311
E09000019
Islington
London
144514
remain
24.8
70.4
101723
52.1
41.7
...
56.8
48.1
670.8
37.2
7.1
31.3
True
True
25227.304
0.174567
324
E09000032
Wandsworth
London
219521
remain
25.0
72.0
158018
67.1
45.1
...
34.2
53.6
718.5
10.6
6.2
29.0
True
True
39504.500
0.179958
320
E09000028
Southwark
London
195875
remain
27.2
66.2
129677
34.1
40.3
...
24.7
43.1
644.2
23.0
8.0
25.8
True
True
35272.144
0.180075
293
E09000001
City of London
London
5987
remain
24.7
73.6
4405
65.7
37.2
...
25.2
68.4
864.7
27.6
3.2
39.9
True
False
1088.035
0.181733
350
S12000036
Edinburgh
Scotland
346073
remain
25.6
73.0
252481
29.2
32.8
...
20.7
41.4
554.2
14.3
NaN
25.6
False
True
64635.136
0.186767
358
S12000046
Glasgow
Scotland
449806
remain
33.4
56.3
253000
63.0
31.9
...
51.8
25.9
515.8
12.3
NaN
18.5
False
True
84502.000
0.187863
60
E07000008
Cambridge
East of England
80108
remain
26.2
72.2
57852
50.8
31.6
...
50.8
47.3
583.3
34.9
11.0
38.0
False
False
15157.224
0.189210
315
E09000023
Lewisham
London
197514
remain
30.1
63.1
124637
61.1
37.4
...
31.5
38.0
606.4
39.1
10.2
22.6
True
True
37515.737
0.189940
331
S12000011
East Renfrewshire
Scotland
69575
remain
25.7
76.1
52969
9.9
21.2
...
12.2
35.2
668.6
12.8
NaN
25.0
False
False
13613.033
0.195660
325
E09000033
Westminster
London
120524
remain
31.0
65.0
78325
15.0
40.0
...
64.8
50.3
764.4
29.0
3.6
26.6
True
True
24280.750
0.201460
312
E09000020
Kensington and Chelsea
London
83042
remain
31.3
66.0
54801
45.8
35.5
...
43.0
52.7
762.8
11.7
3.2
23.4
True
False
17152.713
0.206555
305
E09000013
Hammersmith and Fulham
London
114863
remain
30.0
70.0
80347
48.9
40.6
...
53.6
49.6
686.1
23.6
5.5
27.0
True
True
24104.100
0.209851
322
E09000030
Tower Hamlets
London
167820
remain
32.5
64.6
108420
44.6
45.3
...
18.3
41.0
637.0
16.9
6.6
25.7
True
True
35236.500
0.209966
200
E07000178
Oxford
South East
97331
remain
29.7
72.3
70411
15.0
30.5
...
15.8
42.6
559.0
55.7
11.3
33.6
False
False
20912.067
0.214855
357
S12000045
East Dunbartonshire
Scotland
83031
remain
28.6
75.2
62418
47.3
20.8
...
19.4
34.8
617.5
10.6
NaN
24.6
False
False
17851.548
0.214999
42
E06000043
Brighton and Hove
South East
198293
remain
31.4
74.1
146829
20.6
31.2
...
49.9
36.9
546.6
46.6
10.7
22.7
False
True
46104.306
0.232506
356
S12000044
North Lanarkshire
Scotland
254567
remain
38.3
60.9
155045
31.2
25.7
...
53.7
17.2
511.0
59.1
NaN
12.9
False
True
59382.235
0.233268
259
E08000003
Manchester
North West
338064
remain
39.6
59.8
202067
45.5
33.9
...
60.8
28.9
483.7
52.8
15.4
20.0
False
True
80018.532
0.236696
346
S12000030
Stirling
Scotland
66100
remain
32.3
74.0
48931
24.4
23.1
...
61.3
33.3
511.5
5.4
NaN
20.0
False
False
15804.713
0.239103
336
S12000018
Inverclyde
Scotland
58624
remain
36.2
66.1
38722
26.2
22.8
...
57.3
19.7
518.9
48.5
NaN
15.0
False
False
14017.364
0.239106
345
S12000029
South Lanarkshire
Scotland
248949
remain
36.9
65.4
162683
33.3
24.3
...
38.2
22.0
531.1
29.8
NaN
15.6
False
True
60030.027
0.241134
352
S12000039
West Dunbartonshire
Scotland
67595
remain
38.0
64.0
43245
42.6
24.3
...
55.6
16.5
500.3
7.9
NaN
11.8
False
False
16433.100
0.243111
351
S12000038
Renfrewshire
Scotland
127294
remain
35.2
69.3
88197
10.2
24.5
...
53.7
23.1
534.0
25.3
NaN
15.7
False
True
31045.344
0.243887
340
S12000023
Orkney Islands
Scotland
16658
remain
36.8
68.5
11402
12.4
21.8
...
56.4
26.5
505.2
55.3
NaN
12.7
False
False
4195.936
0.251887
319
E09000027
Richmond-upon-Thames
London
132632
remain
30.7
82.1
108876
4.8
30.9
...
56.4
53.0
744.2
53.6
5.9
29.7
True
True
33424.932
0.252013
355
S12000042
Dundee
Scotland
105554
remain
40.2
62.9
66418
25.3
26.8
...
8.5
24.8
463.3
52.8
NaN
16.5
False
True
26700.036
0.252951
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
282
E08000027
Dudley
West Midlands
244516
leave
67.6
71.7
175333
19.0
24.5
...
45.6
19.2
499.4
43.2
23.8
14.0
False
True
118525.108
0.484734
135
E07000107
Dartford
South East
73951
leave
64.2
75.6
55883
67.1
29.8
...
39.9
22.0
577.2
66.5
14.1
14.6
False
False
35876.886
0.485144
241
E07000224
Arun
South East
117138
leave
62.5
77.9
91199
13.0
20.5
...
24.4
22.8
486.2
19.6
17.0
13.3
False
True
56999.375
0.486600
215
E07000198
Staffordshire Moorlands
West Midlands
79347
leave
64.7
75.4
59793
53.4
21.1
...
46.1
23.7
498.5
50.4
22.3
14.7
False
False
38686.071
0.487556
167
E07000139
North Kesteven
East Midlands
86468
leave
62.3
78.4
67791
35.7
22.7
...
32.2
25.4
509.4
9.1
20.4
14.2
False
False
42233.793
0.488433
127
E07000095
Broxbourne
East of England
68997
leave
66.3
73.8
50907
47.5
26.4
...
37.5
19.2
615.4
9.6
11.3
12.1
False
False
33751.341
0.489171
137
E07000109
Gravesham
South East
72808
leave
65.4
74.9
54553
14.7
26.4
...
15.9
20.0
545.0
52.0
15.9
13.0
False
False
35677.662
0.490024
236
E07000219
Nuneaton and Bedworth
West Midlands
93978
leave
66.0
74.4
69876
64.7
25.2
...
46.1
18.6
488.7
15.5
22.1
12.8
False
False
46118.160
0.490734
209
E07000192
Cannock Chase
West Midlands
75010
leave
68.9
71.5
53607
51.5
25.5
...
49.3
17.2
490.7
21.1
24.1
11.1
False
False
36935.223
0.492404
173
E07000145
Great Yarmouth
East of England
72634
leave
71.5
69.1
50158
67.2
22.3
...
25.8
14.2
448.8
13.2
28.9
10.1
False
False
35862.970
0.493749
106
E07000074
Maldon
East of England
49073
leave
62.6
79.2
38850
4.0
20.2
...
57.9
23.6
609.8
6.7
15.1
15.0
False
False
24320.100
0.495590
174
E07000146
King's Lynn and West Norfolk
East of England
113884
leave
66.4
74.8
85128
15.9
22.0
...
39.2
19.1
468.4
9.0
25.5
12.1
False
True
56524.992
0.496338
216
E07000199
Tamworth
West Midlands
56825
leave
67.5
74.2
42151
67.8
26.2
...
41.3
17.4
472.0
56.4
22.2
11.5
False
False
28451.925
0.500694
105
E07000073
Harlow
East of England
59124
leave
68.1
73.6
43489
59.6
28.0
...
16.5
17.6
490.7
67.3
17.5
12.4
False
False
29616.009
0.500913
213
E07000196
South Staffordshire
West Midlands
85777
leave
64.8
77.8
66739
30.0
21.7
...
52.5
25.1
545.9
36.1
18.2
16.6
False
False
43246.872
0.504178
98
E07000066
Basildon
East of England
132771
leave
68.6
73.9
98062
24.5
26.6
...
66.3
18.6
558.8
15.0
16.2
13.7
False
True
67270.532
0.506666
193
E07000171
Bassetlaw
East Midlands
85547
leave
67.8
74.8
64003
26.5
22.6
...
55.4
20.2
506.3
36.5
23.3
12.8
False
False
43394.034
0.507254
192
E07000170
Ashfield
East Midlands
91916
leave
69.8
72.8
66946
68.4
25.0
...
64.9
15.1
448.9
62.4
26.7
10.6
False
False
46728.308
0.508381
235
E07000218
North Warwickshire
West Midlands
49790
leave
66.9
76.3
37975
15.0
22.9
...
9.6
19.8
492.6
34.3
20.4
13.0
False
False
25405.275
0.510249
72
E07000033
Bolsover
East Midlands
58063
leave
70.8
72.3
41999
13.9
24.3
...
46.2
15.8
428.1
4.2
27.4
10.9
False
False
29735.292
0.512121
196
E07000174
Mansfield
East Midlands
77624
leave
70.9
72.6
56370
11.2
25.3
...
41.8
16.4
435.9
7.0
25.6
11.1
False
False
39966.330
0.514871
108
E07000076
Tendring
East of England
111167
leave
69.5
74.4
82703
35.1
18.1
...
10.4
15.9
493.5
53.4
23.4
10.9
False
True
57478.585
0.517047
107
E07000075
Rochford
East of England
66589
leave
66.6
78.8
52479
29.6
22.0
...
55.8
20.0
577.8
11.2
13.2
14.6
False
False
34951.014
0.524877
33
E06000034
Thurrock
East of England
109897
leave
72.3
72.8
79950
29.0
29.2
...
46.5
17.4
563.7
48.0
15.6
11.3
False
True
57803.850
0.525982
62
E07000010
Fenland
East of England
71447
leave
71.4
73.7
52649
62.8
23.4
...
52.4
14.9
457.7
6.7
26.5
9.4
False
False
37591.386
0.526144
165
E07000137
East Lindsey
East Midlands
107009
leave
70.7
74.9
80178
25.1
18.5
...
44.2
18.1
462.1
53.2
28.8
11.6
False
True
56685.846
0.529730
308
E09000016
Havering
London
183082
leave
69.7
76.0
139175
61.2
26.1
...
21.8
19.4
626.3
22.5
12.3
14.1
True
True
97004.975
0.529844
101
E07000069
Castle Point
East of England
68860
leave
72.7
75.4
51909
21.0
21.1
...
46.5
14.6
565.9
48.4
14.1
11.0
False
False
37737.843
0.548037
168
E07000140
South Holland
East Midlands
65701
leave
73.6
75.4
49518
52.1
22.9
...
67.7
15.8
443.7
31.1
25.9
9.7
False
False
36445.248
0.554714
164
E07000136
Boston
East Midlands
39363
leave
75.6
77.3
30416
14.6
25.6
...
16.6
15.1
411.2
18.9
27.2
10.0
False
False
22994.496
0.584165
381 rows × 21 columns
Content source: puntofisso/jupyter
Similar notebooks: