-
Notifications
You must be signed in to change notification settings - Fork 0
/
mo2020.py
344 lines (252 loc) · 12.1 KB
/
mo2020.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 24 07:27:55 2021
@author: darsh
"""
import pandas as pd
import numpy as np
import csv
#read data + setup
path = 'raw/Files/Final Precinct data 2020 - amendments on same tab as candidates.xlsx'
df = pd.read_excel(path, header = 5, skipfooter=3)
df = pd.DataFrame(df)
#remove blank rows
df = df[['County Name', 'Precinct Code', 'Precinct Name', 'Office Title',
'Candidate Ballot Name', 'Political Party', 'Yes votes', 'No votes']]
#CAPITALIZE and remove extraneous saces
df = df.applymap(lambda x:x.upper() if type(x) == str else x)
df = df.applymap(lambda x:x.strip() if type(x) == str else x)
# ---------------------------------------------------------------------------------------------------------------------------------------------------
# print(df['Office Title'].unique()) #only a few offices in data
def get_office(x):
if 'U.S. PRESIDENT AND VICE PRESIDENT' in x:
return 'US PRESIDENT'
elif 'U.S. REPRESENTATIVE' in x:
return 'US HOUSE'
elif 'STATE REPRESENTATIVE' in x:
return 'STATE HOUSE'
elif 'STATE SENATOR' in x:
return 'STATE SENATE'
elif 'CIRCUIT JUDGE' in x:
return 'CIRCUIT JUDGE'
else:
return x
df['office']=df['Office Title'].apply(get_office)
#---------------------------------------------------------------------------------------------------------------------------------------------------
statewide = ['U.S. PRESIDENT AND VICE PRESIDENT', 'GOVERNOR', 'LIEUTENANT GOVERNOR','SECRETARY OF STATE',
'STATE TREASURER', 'ATTORNEY GENERAL','CONSTITUTIONAL AMENDMENT']
def get_district(x):
if 'CIRCUIT JUDGE' in x:
dist = x[16:]
elif 'DISTRICT' in x:
dist = ''.join([str(n) for n in list(x) if n.isdigit()])
elif any(i in x for i in statewide):
dist = 'STATEWIDE'
else:
return ''
return dist.zfill(3)
df['district'] = df['Office Title'].apply(get_district)
# #---------------------------------------------------------------------------------------------------------------------------------------------------
#Votes also have a 'NO' section for amendment elections, we can melt that down and put it in candidate
df = pd.melt(df, id_vars = ['County Name', 'Precinct Code', 'Precinct Name', 'Office Title',
'Candidate Ballot Name', 'Political Party','office', 'district'],
value_vars = ['Yes votes','No votes'],
var_name = 'y_or_n', value_name = 'votes')
df = df.loc[df['votes'].notnull()] #remove the extra rows for elections with only "yes" (all except the amendments)
def y_or_n(office, y_or_n):
if 'CONSTITUTIONAL AMENDMENT' in office:
if y_or_n == 'Yes votes':
return 'YES'
else:
return 'NO'
else:
return ''
df['y_or_n'] = df.apply(lambda df: y_or_n(df['office'], df['y_or_n']), axis=1)
df['Candidate Ballot Name'].replace(np.nan,'', inplace = True)
df['candidate'] = df['Candidate Ballot Name'].astype(str) + df['y_or_n']
def votes(x):
return int(x)
df['votes'] = df['votes'].apply(votes)
#---------------------------------------------------------------------------------------------------------------------------------------------------
# check = df.loc[df['Candidate Ballot Name'].notnull()]
# check = check.loc[check['Candidate Ballot Name'].str.contains(',')]
# check = check.loc[check['office'] == 'STATE HOUSE']
# print(check['Candidate Ballot Name']) --> only "JOHN D. BOYD, JR." is a non-prez candidate with ","
def candidate(x):
#removing the name of VP from presidents
if type(x) == str and ',' in x and x != 'JOHN D. BOYD, JR.':
return x.split(',')[0]
elif x == 'KASEY WELLS RACHEL WELLS':
return 'KASEY WELLS'
elif x == 'ALISSIA CANADAY':
return 'ALISSIA CANADY'
elif x == 'NARTIN SCHULTE':
return 'MARTIN SCHULTE'
elif x == 'EMMANUEL CLEAVER II':
return 'EMANUEL CLEAVER II'
else:
return x
df['candidate'] = df['candidate'].apply(candidate)
def punctuation(x):
x = str(x)
x = x.replace(".","")
x = x.replace(",","")
x = x.replace("(",'"')
x = x.replace(")",'"')
return x
df['candidate'] = df['candidate'].apply(punctuation)
# #---------------------------------------------------------------------------------------------------------------------------------------------------
# 'WI' under party is write-ins
def writein(x):
if x == 'WI':
return 'TRUE'
else:
return 'FALSE'
df['writein'] = df['Political Party'].apply(writein)
# #---------------------------------------------------------------------------------------------------------------------------------------------------
# print(df['party'].unique()) ---> only ['REP' 'DEM' 'LIB' 'GRE' 'CST' 'WI' 'IND']
def party_det(x):
if x == 'REP':
return 'REPUBLICAN'
elif x == 'DEM':
return 'DEMOCRAT'
elif x == 'LIB':
return 'LIBERTARIAN'
elif x == 'GRE':
return 'GREEN'
elif x == 'WI': #leave party blank for write ins
return ''
elif x == 'CST':
return 'CONSTITUTION'
else:
return 'NONPARTISAN'
df['party_detailed'] = df['Political Party'].apply(party_det)
# #---------------------------------------------------------------------------------------------------------------------------------------------------
main_parties = ['REPUBLICAN','DEMOCRAT','LIBERTARIAN']
other_parties = ['GREEN','CONSTITUTION']
def party_sim(x):
for p in main_parties:
if x == p:
return p
if any(x == i for i in other_parties):
return 'OTHER'
else:
return x
df['party_simplified'] = df['party_detailed'].apply(party_sim)
# #---------------------------------------------------------------------------------------------------------------------------------------------------
state = ['GOVERNOR','LIEUTENANT GOVERNOR','SECRETARY OF STATE','STATE TREASURER','ATTORNEY GENERAL',
'STATE HOUSE','STATE SENATE','CIRCUIT JUDGE','CONSTITUTIONAL AMENDMENT']
def dataverse(x):
if x == 'US PRESIDENT':
return 'PRESIDENT'
elif x == 'US HOUSE':
return 'HOUSE'
elif any(s in x for s in state):
return 'STATE'
else:
return 'LOCAL'
df['dataverse'] = df['office'].apply(dataverse)
#---------------------------------------------------------------------------------------------------------------------------------------------------
#get fips code dataframe for MO counties
df['county'] = df['County Name'].fillna('')
county_fips_path = '../../help-files/county-fips-codes.csv'
fips = pd.read_csv(county_fips_path, delimiter=',', header=0)
fips = pd.DataFrame(fips)
fips = fips.loc[fips['state']=='Missouri']
fips_dict = dict(zip(fips.county_name, fips.county_fips))
fips_dict['']=''
def fips(x):
if x == 'DE KALB': #standardize spelling to the fips format
x = 'DEKALB'
code = fips_dict[x]
return str(code)
df['county_fips'] = df['county'].apply(fips)
df['jurisdiction_fips'] = df['county_fips']
df['county_name'] = df['county']
df['jurisdiction_name'] = df['county']
#---------------------------------------------------------------------------------------------------------------------------------------------------
#MAIN ISSUE: UNDER 'precinct' EACH COUNTY HAS THE SOME ADDITIONAL VALUES AS "PRECINCTS":
#"ABSENTEE" OR "PROVISIONAL" - Net Absentee votes for the county, without precinct level breakdown:
# ACTION -> we will label precicnt as 'COUNTY FLOATING' and the mode 'ABSENTEE'/'PROVISIONAL'
#"FEDERAL"/"MIL/OS ABSENTEES" - Net UOCAVA (Uniformed and Overseas Citizens Absentee Voting Act)
# votes of people from that county, but no precinct level breakdown:
# ACTION -> we will lablel precint as 'COUNTY FLOATING' and mode 'UOCAVA'
def mode(x):
if x == 'FEDERAL' or x == 'MIL/OS ABSENTEES':
return 'UOCAVA'
elif type(x) == str and 'ABSENTEE' in x:
return 'ABSENTEE'
elif type(x) == str and 'PROVISIONAL' in x:
return 'PROVISIONAL'
elif type(x) == str and x=='MAIL-IN':
return 'MAIL'
else:
return 'ELECTION DAY'
df['mode'] = df['Precinct Name'].apply(mode)
def precinct(x):
if type(x) == str:
if 'ABSENTEE' in x or x == 'FEDERAL' or 'PROVISIONAL' in x or x == 'MAIL-IN':
return 'COUNTY FLOATING'
elif any (z in x for z in ['WRITE-IN','WRITE-INS','WRITE INS','WRITE IN','WRITE - IN']):
return 'COUNTYWIDE WI' #standardize ['precinct'] == 'WRITE-IN' rows
else:
return x
else:
return x
#df['precinct'] = df['Precinct Name'].apply(precinct)
df['Precinct Code']=df['Precinct Code'].astype(str)
df['precinct'] = df['Precinct Code'] + '_' + df['Precinct Name'].apply(precinct).astype(str)
#"WRITE-IN" - listed as ['WRITE-IN' 'WRITE-INS' 'WRITE INS' 'WRITE IN','WRITE - IN']
#Irrelevent in pointing out write-ins since in the 'party' column all actual write-ins are marked "WI"
#The check below shows that all "WRITE-IN" 'precincts' that are NOT "WI" under 'party' have 0 votes.
# check = df.loc[df['precinct'] == 'COUNTYWIDE WI']
# check = check.loc[check['writein']=='FALSE']
# print(check['votes'].unique())
# #-------------> returns [0, nan] <---------------
# ACTION -> DELETE THESE by marking which rows to delete
indexNames = df[(df['precinct'] == 'COUNTYWIDE WI') & (df['writein'] == 'FALSE')].index
df.drop(indexNames, inplace = True)
#Only actual write-in candidates have non-zero votes in 3 different ways:
# 1) Votes under precinct breakdown are NaN, there is a county vote total under ['precinct'] == 'WRITE-IN'
# ACTION --> Delete the NaN values and kept the total
# 2) Votes ARE under precinct breakdown, the ['precinct'] == 'WRITE-IN' is NaN
# ACTION --> Delete the "Total" which is null, keep precinct breakdown
# 3) Votes are under BOTH precinct breakdown and "Total" under ['precinct'] == 'WRITE-IN'
# ACTION --> Check if the votes add up, if yes delete "Total" Row to avoid double counting
# if no mark 'TRUE' in readme_check
#dropping rows with writeins having 0 votes
indexNames = df[(df['writein'] == 'TRUE') & (df['votes']==0)].index
df.drop(indexNames, inplace = True)
to_check = df.loc[df['writein'] == 'TRUE']
to_check = to_check[['county','precinct','candidate','votes','mode']]
# print(to_check) #---> only 133 rows, check by creating its own csv --.
# # |
# to_check.to_csv('check-write-ins.csv', index = False) #<---'
#---> after dropping votes = 0 all counties are:
#either only total county floating write-in or
#write-in with precinct level breakdown
#No chance of double counting! Yay!
#go back to replace 'COUNTYWIDE WI" with "COUNTY FLOATING"
df['precinct'].replace('COUNTYWIDE WI', 'COUNTY FLOATING', inplace=True)
#---------------------------------------------------------------------------------------------------------------------------------------------------
#all only have one winner from lookig at unique values of office and candidates
df['magnitude'] = '1'
#sec of state website and ballotopedia indicated no special elections
df['year']='2020'
df['date']='2020-11-03'
df['special'] = 'FALSE'
df['stage'] = 'GEN'
df['state'] = 'MISSOURI'
df['state_po']='MO'
df['state_fips']= '29'
df['state_cen']= '43'
df['state_ic']= '34'
df['readme_check']='FALSE'
df = df[["precinct", "office", "party_detailed", "party_simplified", "mode", "votes",
"candidate", "district", "dataverse","state", "special", "writein","date","year",
"county_name","county_fips", "jurisdiction_name","jurisdiction_fips", "stage", "state_po",
"state_fips", "state_cen", "state_ic", "readme_check", 'magnitude']]
df = df.applymap(lambda x:x.strip() if type(x) == str else x)
df.fillna('', inplace=True)
df.to_csv('2020-mo-precinct-general.csv', index = False, quoting=csv.QUOTE_NONNUMERIC)