-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNC_election_reports.py
347 lines (316 loc) · 23.4 KB
/
NC_election_reports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# Import modules
import pandas as pd
import numpy as np
pd.options.mode.chained_assignment = None #hide SettingWithCopyWarning
########## 2016 ##########
# Registrants (registered voters in 2016)
Reg_data_2016 = pd.read_csv('https://s3.amazonaws.com/dl.ncsbe.gov/ENRS/2016_11_08/voter_stats_20161108.zip', sep='\t')
Reg_data_2016['party_cd'] = Reg_data_2016['party_cd'].str.replace('DEM','reg_DEM')
Reg_data_2016['party_cd'] = Reg_data_2016['party_cd'].str.replace('REP','reg_REP')
Reg_data_2016['party_cd'] = Reg_data_2016['party_cd'].str.replace('UNA','reg_UNA')
Reg_data_2016['party_cd'] = Reg_data_2016['party_cd'].str.replace('LIB','reg_LIB')
Reg_data_2016_table2 = pd.pivot_table(Reg_data_2016, values='total_voters', index=['county_desc'], columns=['party_cd'], aggfunc=np.sum)
Reg_data_2016_table3 = pd.pivot_table(Reg_data_2016, values='total_voters', index=['county_desc'], columns=['election_date'], aggfunc=np.sum)
Reg_data_2016_table = pd.merge(Reg_data_2016_table2, Reg_data_2016_table3, on=['county_desc'])
Reg_data_2016_table = pd.DataFrame(Reg_data_2016_table.to_records()) #Flatten pivot table into dataframe
Reg_data_2016_table = Reg_data_2016_table.rename(columns={'11/08/2016':'reg_Total'})
# Voters (overall turnout in 2016 including election day and AB/EV)
Turnout_data_2016 = pd.read_csv('https://s3.amazonaws.com/dl.ncsbe.gov/ENRS/2016_11_08/history_stats_20161108.zip', sep='\t')
Turnout_data_2016['party_cd'] = Turnout_data_2016['party_cd'].str.replace('DEM','turn_DEM')
Turnout_data_2016['party_cd'] = Turnout_data_2016['party_cd'].str.replace('REP','turn_REP')
Turnout_data_2016['party_cd'] = Turnout_data_2016['party_cd'].str.replace('UNA','turn_UNA')
Turnout_data_2016['party_cd'] = Turnout_data_2016['party_cd'].str.replace('LIB','turn_LIB')
Turnout_data_2016_table2 = pd.pivot_table(Turnout_data_2016, values='total_voters', index=['county_desc'], columns=['party_cd'], aggfunc=np.sum)
Turnout_data_2016_table3 = pd.pivot_table(Turnout_data_2016, values='total_voters', index=['county_desc'], columns=['election_date'], aggfunc=np.sum)
Turnout_data_2016_table = pd.merge(Turnout_data_2016_table2, Turnout_data_2016_table3, on=['county_desc'])
Turnout_data_2016_table = pd.DataFrame(Turnout_data_2016_table.to_records()) #Flatten pivot table into dataframe
Turnout_data_2016_table = Turnout_data_2016_table.rename(columns={'11/08/2016':'turn_Total'})
# Absentees (AB or EV ballots cast in 2016)
AB_data_2016 = pd.read_csv('https://s3.amazonaws.com/dl.ncsbe.gov/ENRS/2016_11_08/absentee_counts_county_20161108.csv', header=None)
AB_data_2016.columns = ['county_desc', 'voting_method_desc', 'party_cd', 'sex_code', 'race_code', 'total_voters']
AB_data_2016['party_cd'] = AB_data_2016['party_cd'].str.replace('DEM','ab_DEM')
AB_data_2016['party_cd'] = AB_data_2016['party_cd'].str.replace('REP','ab_REP')
AB_data_2016['party_cd'] = AB_data_2016['party_cd'].str.replace('UNA','ab_UNA')
AB_data_2016['party_cd'] = AB_data_2016['party_cd'].str.replace('LIB','ab_LIB')
AB_data_2016_table2 = pd.pivot_table(AB_data_2016, values='total_voters', index=['county_desc'], columns=['party_cd'], aggfunc=np.sum)
AB_data_2016_table3 = pd.pivot_table(AB_data_2016, values='total_voters', index=['county_desc'], aggfunc=np.sum)
AB_data_2016_table = pd.merge(AB_data_2016_table2, AB_data_2016_table3, on=['county_desc'])
AB_data_2016_table = pd.DataFrame(AB_data_2016_table.to_records()) #Flatten pivot table into dataframe
AB_data_2016_table = AB_data_2016_table.rename(columns={'total_voters':'ab_Total'})
# Merge Registration, Turnout, & AB tables by county name
Data_2016 = pd.merge(Reg_data_2016_table, Turnout_data_2016_table, on=['county_desc'])
Data_2016 = pd.merge(Data_2016, AB_data_2016_table, on=['county_desc'])
# Breakdown counties by urban, suburban, or rural, based on population size
top4_2016 = Data_2016.loc[(Data_2016['county_desc'] == 'WAKE') | (Data_2016['county_desc'] == 'GUILFORD') | (Data_2016['county_desc'] == 'MECKLENBURG') | (Data_2016['county_desc'] == 'FORSYTH')]
top4_2016.loc['Urban4',1:] = top4_2016.sum(axis=0)
top4_2016.drop(top4_2016.head(4).index,inplace=True)
mid16_2016 = Data_2016.loc[(Data_2016['county_desc'] == 'ALAMANCE') |
(Data_2016['county_desc'] == 'BUNCOMBE') |
(Data_2016['county_desc'] == 'CABARRUS') |
(Data_2016['county_desc'] == 'CATAWBA') |
(Data_2016['county_desc'] == 'CUMBERLAND') |
(Data_2016['county_desc'] == 'DAVIDSON') |
(Data_2016['county_desc'] == 'DURHAM') |
(Data_2016['county_desc'] == 'GASTON') |
(Data_2016['county_desc'] == 'IREDELL') |
(Data_2016['county_desc'] == 'JOHNSTON') |
(Data_2016['county_desc'] == 'NEW HANOVER') |
(Data_2016['county_desc'] == 'ONSLOW') |
(Data_2016['county_desc'] == 'ORANGE') |
(Data_2016['county_desc'] == 'PITT') |
(Data_2016['county_desc'] == 'UNION') |
(Data_2016['county_desc'] == 'WATAUGA')]
mid16_2016.loc['Suburban16',1:] = mid16_2016.sum(axis=0)
mid16_2016.drop(mid16_2016.head(16).index,inplace=True)
bottom80_2016 = Data_2016.loc[(Data_2016['county_desc'] != 'ALAMANCE') &
(Data_2016['county_desc'] != 'BUNCOMBE') &
(Data_2016['county_desc'] != 'CABARRUS') &
(Data_2016['county_desc'] != 'CATAWBA') &
(Data_2016['county_desc'] != 'CUMBERLAND') &
(Data_2016['county_desc'] != 'DAVIDSON') &
(Data_2016['county_desc'] != 'DURHAM') &
(Data_2016['county_desc'] != 'GASTON') &
(Data_2016['county_desc'] != 'IREDELL') &
(Data_2016['county_desc'] != 'JOHNSTON') &
(Data_2016['county_desc'] != 'NEW HANOVER') &
(Data_2016['county_desc'] != 'ONSLOW') &
(Data_2016['county_desc'] != 'ORANGE') &
(Data_2016['county_desc'] != 'PITT') &
(Data_2016['county_desc'] != 'UNION') &
(Data_2016['county_desc'] != 'WATAUGA') &
(Data_2016['county_desc'] != 'WAKE') &
(Data_2016['county_desc'] != 'MECKLENBURG') &
(Data_2016['county_desc'] != 'GUILFORD') &
(Data_2016['county_desc'] != 'FORSYTH')]
bottom80_2016.loc['Rural80',1:] = bottom80_2016.sum(axis=0)
bottom80_2016.drop(bottom80_2016.head(80).index,inplace=True)
#Combine urban, suburban, rural dataframes
frames = [top4_2016, mid16_2016, bottom80_2016]
geo2016 = pd.concat(frames)
geo2016.loc['Statewide',1:] = geo2016.sum(axis=0)
newcol = ['Urban4', 'Suburban16', 'Rural80', 'Statewide']
geo2016['county_desc'] = newcol
geo2016 = geo2016.reset_index()
del geo2016['index']
# Statewide totals for each column in county breakdown
Data_2016.loc['Statewide',1:] = Data_2016.sum(axis=0)
Data_2016['county_desc'] = Data_2016['county_desc'].replace(np.nan, 'Statewide', regex=True)
Data_2016 = Data_2016.reset_index()
del Data_2016['index']
# Calculated fields for county breakdown
Data_2016['DEM%_of_registrants'] = (100 * Data_2016['reg_DEM'] / Data_2016['reg_Total']).round(2)
Data_2016['REP%_of_registrants'] = (100 * Data_2016['reg_REP'] / Data_2016['reg_Total']).round(2)
Data_2016['UNA%_of_registrants'] = (100 * Data_2016['reg_UNA'] / Data_2016['reg_Total']).round(2)
Data_2016['LIB%_of_registrants'] = (100 * Data_2016['reg_LIB'] / Data_2016['reg_Total']).round(2)
Data_2016['%DEM_voting'] = (100 * Data_2016['turn_DEM'] / Data_2016['reg_DEM']).round(2)
Data_2016['%REP_voting'] = (100 * Data_2016['turn_REP'] / Data_2016['reg_REP']).round(2)
Data_2016['%UNA_voting'] = (100 * Data_2016['turn_UNA'] / Data_2016['reg_UNA']).round(2)
Data_2016['%LIB_voting'] = (100 * Data_2016['turn_LIB'] / Data_2016['reg_LIB']).round(2)
Data_2016['DEM%_of_AB'] = (100 * Data_2016['ab_DEM'] / Data_2016['ab_Total']).round(2)
Data_2016['REP%_of_AB'] = (100 * Data_2016['ab_REP'] / Data_2016['ab_Total']).round(2)
Data_2016['UNA%_of_AB'] = (100 * Data_2016['ab_UNA'] / Data_2016['ab_Total']).round(2)
Data_2016['LIB%_of_AB'] = (100 * Data_2016['ab_LIB'] / Data_2016['ab_Total']).round(2)
Data_2016['DEM%_of_votes'] = (100 * Data_2016['turn_DEM'] / Data_2016['turn_Total']).round(2)
Data_2016['REP%_of_votes'] = (100 * Data_2016['turn_REP'] / Data_2016['turn_Total']).round(2)
Data_2016['UNA%_of_votes'] = (100 * Data_2016['turn_UNA'] / Data_2016['turn_Total']).round(2)
Data_2016['LIB%_of_votes'] = (100 * Data_2016['turn_LIB'] / Data_2016['turn_Total']).round(2)
Data_2016['DEM_votes%-reg%'] = Data_2016['DEM%_of_votes'] - Data_2016['DEM%_of_registrants']
Data_2016['REP_votes%-reg%'] = Data_2016['REP%_of_votes'] - Data_2016['REP%_of_registrants']
Data_2016['UNA_votes%-reg%'] = Data_2016['UNA%_of_votes'] - Data_2016['UNA%_of_registrants']
Data_2016['LIB_votes%-reg%'] = Data_2016['LIB%_of_votes'] - Data_2016['LIB%_of_registrants']
# Calculated fields for geographic breakdown
geo2016['DEM%_of_registrants'] = (100 * geo2016['reg_DEM'] / geo2016['reg_Total']).round(2)
geo2016['REP%_of_registrants'] = (100 * geo2016['reg_REP'] / geo2016['reg_Total']).round(2)
geo2016['UNA%_of_registrants'] = (100 * geo2016['reg_UNA'] / geo2016['reg_Total']).round(2)
geo2016['LIB%_of_registrants'] = (100 * geo2016['reg_LIB'] / geo2016['reg_Total']).round(2)
geo2016['%DEM_voting'] = (100 * geo2016['turn_DEM'] / geo2016['reg_DEM']).round(2)
geo2016['%REP_voting'] = (100 * geo2016['turn_REP'] / geo2016['reg_REP']).round(2)
geo2016['%UNA_voting'] = (100 * geo2016['turn_UNA'] / geo2016['reg_UNA']).round(2)
geo2016['%LIB_voting'] = (100 * geo2016['turn_LIB'] / geo2016['reg_LIB']).round(2)
geo2016['DEM%_of_AB'] = (100 * geo2016['ab_DEM'] / geo2016['ab_Total']).round(2)
geo2016['REP%_of_AB'] = (100 * geo2016['ab_REP'] / geo2016['ab_Total']).round(2)
geo2016['UNA%_of_AB'] = (100 * geo2016['ab_UNA'] / geo2016['ab_Total']).round(2)
geo2016['LIB%_of_AB'] = (100 * geo2016['ab_LIB'] / geo2016['ab_Total']).round(2)
geo2016['DEM%_of_votes'] = (100 * geo2016['turn_DEM'] / geo2016['turn_Total']).round(2)
geo2016['REP%_of_votes'] = (100 * geo2016['turn_REP'] / geo2016['turn_Total']).round(2)
geo2016['UNA%_of_votes'] = (100 * geo2016['turn_UNA'] / geo2016['turn_Total']).round(2)
geo2016['LIB%_of_votes'] = (100 * geo2016['turn_LIB'] / geo2016['turn_Total']).round(2)
geo2016['DEM_votes%-reg%'] = geo2016['DEM%_of_votes'] - geo2016['DEM%_of_registrants']
geo2016['REP_votes%-reg%'] = geo2016['REP%_of_votes'] - geo2016['REP%_of_registrants']
geo2016['UNA_votes%-reg%'] = geo2016['UNA%_of_votes'] - geo2016['UNA%_of_registrants']
geo2016['LIB_votes%-reg%'] = geo2016['LIB%_of_votes'] - geo2016['LIB%_of_registrants']
# Write 2016 county breakdown to CSV file in working directory
Data_2016.to_csv('NC2016countyreport.csv', sep=',', encoding='utf-8', header='true')
# Write 2016 geographic breakdown to CSV file in working directory
geo2016.to_csv('NC2016georeport.csv', sep=',', encoding='utf-8', header='true')
########## 2020 ##########
# Registrants (registered voters in 2020)
Reg_data_2020 = pd.read_csv('https://s3.amazonaws.com/dl.ncsbe.gov/ENRS/2020_11_03/voter_stats_20201103.zip', sep='\t')
Reg_data_2020['party_cd'] = Reg_data_2020['party_cd'].str.replace('DEM','reg_DEM')
Reg_data_2020['party_cd'] = Reg_data_2020['party_cd'].str.replace('REP','reg_REP')
Reg_data_2020['party_cd'] = Reg_data_2020['party_cd'].str.replace('UNA','reg_UNA')
Reg_data_2020['party_cd'] = Reg_data_2020['party_cd'].str.replace('LIB','reg_LIB')
Reg_data_2020['party_cd'] = Reg_data_2020['party_cd'].str.replace('CST','reg_CST')
Reg_data_2020['party_cd'] = Reg_data_2020['party_cd'].str.replace('GRE','reg_GRE')
Reg_data_2020_table2 = pd.pivot_table(Reg_data_2020, values='total_voters', index=['county_desc'], columns=['party_cd'], aggfunc=np.sum)
Reg_data_2020_table3 = pd.pivot_table(Reg_data_2020, values='total_voters', index=['county_desc'], columns=['election_date'], aggfunc=np.sum)
Reg_data_2020_table = pd.merge(Reg_data_2020_table2, Reg_data_2020_table3, on=['county_desc'])
Reg_data_2020_table = pd.DataFrame(Reg_data_2020_table.to_records()) #Flatten pivot table into dataframe
Reg_data_2020_table = Reg_data_2020_table.rename(columns={'11/03/2020':'reg_Total'})
# Voters (overall turnout in 2020 including election day and AB/EV)
Turnout_data_2020 = pd.read_csv('https://s3.amazonaws.com/dl.ncsbe.gov/ENRS/2020_11_03/history_stats_20201103.zip', sep='\t')
Turnout_data_2020['party_cd'] = Turnout_data_2020['party_cd'].str.replace('DEM','turn_DEM')
Turnout_data_2020['party_cd'] = Turnout_data_2020['party_cd'].str.replace('REP','turn_REP')
Turnout_data_2020['party_cd'] = Turnout_data_2020['party_cd'].str.replace('UNA','turn_UNA')
Turnout_data_2020['party_cd'] = Turnout_data_2020['party_cd'].str.replace('LIB','turn_LIB')
Turnout_data_2020['party_cd'] = Turnout_data_2020['party_cd'].str.replace('CST','turn_CST')
Turnout_data_2020['party_cd'] = Turnout_data_2020['party_cd'].str.replace('GRE','turn_GRE')
Turnout_data_2020_table2 = pd.pivot_table(Turnout_data_2020, values='total_voters', index=['county_desc'], columns=['party_cd'], aggfunc=np.sum)
Turnout_data_2020_table3 = pd.pivot_table(Turnout_data_2020, values='total_voters', index=['county_desc'], columns=['election_date'], aggfunc=np.sum)
Turnout_data_2020_table = pd.merge(Turnout_data_2020_table2, Turnout_data_2020_table3, on=['county_desc'])
Turnout_data_2020_table = pd.DataFrame(Turnout_data_2020_table.to_records()) #Flatten pivot table into dataframe
Turnout_data_2020_table = Turnout_data_2020_table.rename(columns={'11/03/2020':'turn_Total'})
# Absentees (AB or EV ballots cast in 2020)
AB_data_2020 = pd.read_csv('https://s3.amazonaws.com/dl.ncsbe.gov/ENRS/2020_11_03/absentee_counts_county_20201103.csv', header=None)
AB_data_2020.columns = ['county_desc', 'voting_method_desc', 'party_cd', 'sex_code', 'race_code', 'total_voters']
AB_data_2020['party_cd'] = AB_data_2020['party_cd'].str.replace('DEM','ab_DEM')
AB_data_2020['party_cd'] = AB_data_2020['party_cd'].str.replace('REP','ab_REP')
AB_data_2020['party_cd'] = AB_data_2020['party_cd'].str.replace('UNA','ab_UNA')
AB_data_2020['party_cd'] = AB_data_2020['party_cd'].str.replace('LIB','ab_LIB')
AB_data_2020['party_cd'] = AB_data_2020['party_cd'].str.replace('CST','ab_CST')
AB_data_2020['party_cd'] = AB_data_2020['party_cd'].str.replace('GRE','ab_GRE')
AB_data_2020_table2 = pd.pivot_table(AB_data_2020, values='total_voters', index=['county_desc'], columns=['party_cd'], aggfunc=np.sum)
AB_data_2020_table3 = pd.pivot_table(AB_data_2020, values='total_voters', index=['county_desc'], aggfunc=np.sum)
AB_data_2020_table = pd.merge(AB_data_2020_table2, AB_data_2020_table3, on=['county_desc'])
AB_data_2020_table = pd.DataFrame(AB_data_2020_table.to_records()) #Flatten pivot table into dataframe
AB_data_2020_table = AB_data_2020_table.rename(columns={'total_voters':'ab_Total'})
# Merge Registration, Turnout, & AB tables by county name
Data_2020 = pd.merge(Reg_data_2020_table, Turnout_data_2020_table, on=['county_desc'])
Data_2020 = pd.merge(Data_2020, AB_data_2020_table, on=['county_desc'])
# Breakdown counties by urban, suburban, or rural, based on population size
top4_2020 = Data_2020.loc[(Data_2020['county_desc'] == 'WAKE') | (Data_2020['county_desc'] == 'GUILFORD') | (Data_2020['county_desc'] == 'MECKLENBURG') | (Data_2020['county_desc'] == 'FORSYTH')]
top4_2020.loc['Urban4',1:] = top4_2020.sum(axis=0)
top4_2020.drop(top4_2020.head(4).index,inplace=True)
mid16_2020 = Data_2020.loc[(Data_2020['county_desc'] == 'ALAMANCE') |
(Data_2020['county_desc'] == 'BUNCOMBE') |
(Data_2020['county_desc'] == 'CABARRUS') |
(Data_2020['county_desc'] == 'CATAWBA') |
(Data_2020['county_desc'] == 'CUMBERLAND') |
(Data_2020['county_desc'] == 'DAVIDSON') |
(Data_2020['county_desc'] == 'DURHAM') |
(Data_2020['county_desc'] == 'GASTON') |
(Data_2020['county_desc'] == 'IREDELL') |
(Data_2020['county_desc'] == 'JOHNSTON') |
(Data_2020['county_desc'] == 'NEW HANOVER') |
(Data_2020['county_desc'] == 'ONSLOW') |
(Data_2020['county_desc'] == 'ORANGE') |
(Data_2020['county_desc'] == 'PITT') |
(Data_2020['county_desc'] == 'UNION') |
(Data_2020['county_desc'] == 'WATAUGA')]
mid16_2020.loc['Suburban16',1:] = mid16_2020.sum(axis=0)
mid16_2020.drop(mid16_2020.head(16).index,inplace=True)
bottom80_2020 = Data_2020.loc[(Data_2020['county_desc'] != 'ALAMANCE') &
(Data_2020['county_desc'] != 'BUNCOMBE') &
(Data_2020['county_desc'] != 'CABARRUS') &
(Data_2020['county_desc'] != 'CATAWBA') &
(Data_2020['county_desc'] != 'CUMBERLAND') &
(Data_2020['county_desc'] != 'DAVIDSON') &
(Data_2020['county_desc'] != 'DURHAM') &
(Data_2020['county_desc'] != 'GASTON') &
(Data_2020['county_desc'] != 'IREDELL') &
(Data_2020['county_desc'] != 'JOHNSTON') &
(Data_2020['county_desc'] != 'NEW HANOVER') &
(Data_2020['county_desc'] != 'ONSLOW') &
(Data_2020['county_desc'] != 'ORANGE') &
(Data_2020['county_desc'] != 'PITT') &
(Data_2020['county_desc'] != 'UNION') &
(Data_2020['county_desc'] != 'WATAUGA') &
(Data_2020['county_desc'] != 'WAKE') &
(Data_2020['county_desc'] != 'MECKLENBURG') &
(Data_2020['county_desc'] != 'GUILFORD') &
(Data_2020['county_desc'] != 'FORSYTH')]
bottom80_2020.loc['Rural80',1:] = bottom80_2020.sum(axis=0)
bottom80_2020.drop(bottom80_2020.head(80).index,inplace=True)
#Combine urban, suburban, rural dataframes
frames = [top4_2020, mid16_2020, bottom80_2020]
geo2020 = pd.concat(frames)
geo2020.loc['Statewide',1:] = geo2020.sum(axis=0)
newcol = ['Urban4', 'Suburban16', 'Rural80', 'Statewide']
geo2020['county_desc'] = newcol
geo2020 = geo2020.reset_index()
del geo2020['index']
# Statewide totals for each column in county breakdown
Data_2020.loc['Statewide',1:] = Data_2020.sum(axis=0)
Data_2020['county_desc'] = Data_2020['county_desc'].replace(np.nan, 'Statewide', regex=True)
Data_2020 = Data_2020.reset_index()
del Data_2020['index']
# Calculated fields for county breakdown
Data_2020['DEM%_of_registrants'] = (100 * Data_2020['reg_DEM'] / Data_2020['reg_Total']).round(2)
Data_2020['REP%_of_registrants'] = (100 * Data_2020['reg_REP'] / Data_2020['reg_Total']).round(2)
Data_2020['UNA%_of_registrants'] = (100 * Data_2020['reg_UNA'] / Data_2020['reg_Total']).round(2)
Data_2020['LIB%_of_registrants'] = (100 * Data_2020['reg_LIB'] / Data_2020['reg_Total']).round(2)
Data_2020['CST%_of_registrants'] = (100 * Data_2020['reg_CST'] / Data_2020['reg_Total']).round(2)
Data_2020['GRE%_of_registrants'] = (100 * Data_2020['reg_GRE'] / Data_2020['reg_Total']).round(2)
Data_2020['%DEM_voting'] = (100 * Data_2020['turn_DEM'] / Data_2020['reg_DEM']).round(2)
Data_2020['%REP_voting'] = (100 * Data_2020['turn_REP'] / Data_2020['reg_REP']).round(2)
Data_2020['%UNA_voting'] = (100 * Data_2020['turn_UNA'] / Data_2020['reg_UNA']).round(2)
Data_2020['%LIB_voting'] = (100 * Data_2020['turn_LIB'] / Data_2020['reg_LIB']).round(2)
Data_2020['%CST_voting'] = (100 * Data_2020['turn_CST'] / Data_2020['reg_CST']).round(2)
Data_2020['%GRE_voting'] = (100 * Data_2020['turn_GRE'] / Data_2020['reg_GRE']).round(2)
Data_2020['DEM%_of_AB'] = (100 * Data_2020['ab_DEM'] / Data_2020['ab_Total']).round(2)
Data_2020['REP%_of_AB'] = (100 * Data_2020['ab_REP'] / Data_2020['ab_Total']).round(2)
Data_2020['UNA%_of_AB'] = (100 * Data_2020['ab_UNA'] / Data_2020['ab_Total']).round(2)
Data_2020['LIB%_of_AB'] = (100 * Data_2020['ab_LIB'] / Data_2020['ab_Total']).round(2)
Data_2020['CST%_of_AB'] = (100 * Data_2020['ab_CST'] / Data_2020['ab_Total']).round(2)
Data_2020['GRE%_of_AB'] = (100 * Data_2020['ab_GRE'] / Data_2020['ab_Total']).round(2)
Data_2020['DEM%_of_votes'] = (100 * Data_2020['turn_DEM'] / Data_2020['turn_Total']).round(2)
Data_2020['REP%_of_votes'] = (100 * Data_2020['turn_REP'] / Data_2020['turn_Total']).round(2)
Data_2020['UNA%_of_votes'] = (100 * Data_2020['turn_UNA'] / Data_2020['turn_Total']).round(2)
Data_2020['LIB%_of_votes'] = (100 * Data_2020['turn_LIB'] / Data_2020['turn_Total']).round(2)
Data_2020['CST%_of_votes'] = (100 * Data_2020['turn_CST'] / Data_2020['turn_Total']).round(2)
Data_2020['GRE%_of_votes'] = (100 * Data_2020['turn_GRE'] / Data_2020['turn_Total']).round(2)
Data_2020['DEM_votes%-reg%'] = Data_2020['DEM%_of_votes'] - Data_2020['DEM%_of_registrants']
Data_2020['REP_votes%-reg%'] = Data_2020['REP%_of_votes'] - Data_2020['REP%_of_registrants']
Data_2020['UNA_votes%-reg%'] = Data_2020['UNA%_of_votes'] - Data_2020['UNA%_of_registrants']
Data_2020['LIB_votes%-reg%'] = Data_2020['LIB%_of_votes'] - Data_2020['LIB%_of_registrants']
Data_2020['CST_votes%-reg%'] = Data_2020['CST%_of_votes'] - Data_2020['CST%_of_registrants']
Data_2020['GRE_votes%-reg%'] = Data_2020['GRE%_of_votes'] - Data_2020['GRE%_of_registrants']
# Calculated fields for geographic breakdown
geo2020['DEM%_of_registrants'] = (100 * geo2020['reg_DEM'] / geo2020['reg_Total']).round(2)
geo2020['REP%_of_registrants'] = (100 * geo2020['reg_REP'] / geo2020['reg_Total']).round(2)
geo2020['UNA%_of_registrants'] = (100 * geo2020['reg_UNA'] / geo2020['reg_Total']).round(2)
geo2020['LIB%_of_registrants'] = (100 * geo2020['reg_LIB'] / geo2020['reg_Total']).round(2)
geo2020['CST%_of_registrants'] = (100 * geo2020['reg_CST'] / geo2020['reg_Total']).round(2)
geo2020['GRE%_of_registrants'] = (100 * geo2020['reg_GRE'] / geo2020['reg_Total']).round(2)
geo2020['%DEM_voting'] = (100 * geo2020['turn_DEM'] / geo2020['reg_DEM']).round(2)
geo2020['%REP_voting'] = (100 * geo2020['turn_REP'] / geo2020['reg_REP']).round(2)
geo2020['%UNA_voting'] = (100 * geo2020['turn_UNA'] / geo2020['reg_UNA']).round(2)
geo2020['%LIB_voting'] = (100 * geo2020['turn_LIB'] / geo2020['reg_LIB']).round(2)
geo2020['%CST_voting'] = (100 * geo2020['turn_CST'] / geo2020['reg_CST']).round(2)
geo2020['%GRE_voting'] = (100 * geo2020['turn_GRE'] / geo2020['reg_GRE']).round(2)
geo2020['DEM%_of_AB'] = (100 * geo2020['ab_DEM'] / geo2020['ab_Total']).round(2)
geo2020['REP%_of_AB'] = (100 * geo2020['ab_REP'] / geo2020['ab_Total']).round(2)
geo2020['UNA%_of_AB'] = (100 * geo2020['ab_UNA'] / geo2020['ab_Total']).round(2)
geo2020['LIB%_of_AB'] = (100 * geo2020['ab_LIB'] / geo2020['ab_Total']).round(2)
geo2020['CST%_of_AB'] = (100 * geo2020['ab_CST'] / geo2020['ab_Total']).round(2)
geo2020['GRE%_of_AB'] = (100 * geo2020['ab_GRE'] / geo2020['ab_Total']).round(2)
geo2020['DEM%_of_votes'] = (100 * geo2020['turn_DEM'] / geo2020['turn_Total']).round(2)
geo2020['REP%_of_votes'] = (100 * geo2020['turn_REP'] / geo2020['turn_Total']).round(2)
geo2020['UNA%_of_votes'] = (100 * geo2020['turn_UNA'] / geo2020['turn_Total']).round(2)
geo2020['LIB%_of_votes'] = (100 * geo2020['turn_LIB'] / geo2020['turn_Total']).round(2)
geo2020['CST%_of_votes'] = (100 * geo2020['turn_CST'] / geo2020['turn_Total']).round(2)
geo2020['GRE%_of_votes'] = (100 * geo2020['turn_GRE'] / geo2020['turn_Total']).round(2)
geo2020['DEM_votes%-reg%'] = geo2020['DEM%_of_votes'] - geo2020['DEM%_of_registrants']
geo2020['REP_votes%-reg%'] = geo2020['REP%_of_votes'] - geo2020['REP%_of_registrants']
geo2020['UNA_votes%-reg%'] = geo2020['UNA%_of_votes'] - geo2020['UNA%_of_registrants']
geo2020['LIB_votes%-reg%'] = geo2020['LIB%_of_votes'] - geo2020['LIB%_of_registrants']
geo2020['CST_votes%-reg%'] = geo2020['CST%_of_votes'] - geo2020['CST%_of_registrants']
geo2020['GRE_votes%-reg%'] = geo2020['GRE%_of_votes'] - geo2020['GRE%_of_registrants']
# Write 2020 county breakdown to CSV file in working directory
Data_2020.to_csv('NC2020countyreport.csv', sep=',', encoding='utf-8', header='true')
# Write 2020 geographic breakdown to CSV file in working directory
geo2020.to_csv('NC2020georeport.csv', sep=',', encoding='utf-8', header='true')
print(Data_2016)
print(geo2016)
print(Data_2020)
print(geo2020)