-
Notifications
You must be signed in to change notification settings - Fork 0
/
ak2020.py
317 lines (253 loc) · 11.4 KB
/
ak2020.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 31 17:17:54 2021
@author: jennifermiranda
"""
import pandas as pd
import numpy as np
import csv
df = pd.read_csv('raw/resultsbyprecinct.txt', delimiter=",")
df = df.fillna("")
df = df.rename(columns={"Race Statistics": "office", "Number of Precincts": "candidate", "HD99 Fed Overseas Absentee ": "precinct", "NP.2": "party_detailed", "0": "votes"})
df = df.drop([ 'Unnamed: 8', 'NP', 'NP.1', 'Total'], axis=1)
# Use merge_on_statecodes.csv to get info for AK:
df["state"] = 'ALASKA'
df["state_po"] = 'AK'
df["state_fips"] = '02'
df["state_cen"] = '94'
df["state_ic"] = '81'
df["votes"] = df["votes"].apply(str)
df["stage"] = "GEN"
df["date"] = "2020-11-03"
df["year"] = "2020"
df["mode"] = "ELECTION DAY"
df.loc[df['precinct'].str.contains('Early Voting'), 'mode'] = 'EARLY VOTING'
df.loc[df['precinct'].str.contains('Absentee'), 'mode'] = 'ABSENTEE'
df["special"] = "FALSE"
df['writein'] = 'FALSE'
df['county_name'] = ""
df['county_fips'] = ""
df['magnitude'] = 1
def makeDummyColumn(x):
if ('Absentee - 1' in x or 'Absentee - 2' in x) and len(x) >= 49:
x = x[:-27]
elif ('Absentee - 1' in x or 'Absentee - 2' in x) and len(x) < 49:
x = x[:-26]
elif ('Early Voting - 1' in x or 'Early Voting - 2' in x) and len(x) >= 57:
x = x[:-31]
elif ('Early Voting - 1' in x or 'Early Voting - 2' in x) and len(x) < 57:
x = x[:-30]
elif ('Question - 1' in x or 'Question - 2' in x) and len(x) >= 50:
x = x[:-28]
elif ('Question - 1' in x or 'Question - 2' in x) and len(x) < 50:
x = x[:-27]
elif '32-847' in x:
x = '32-847 Seldovia/Kachemak Bay'
elif '31-370' in x:
x = '31-370 Kachemak/Fritz Creek'
else:
return x
return x
df['precinct_dummy'] = df['precinct'].apply(makeDummyColumn)
df['precinct_dummy'] = df['precinct_dummy'].str.replace('\s+', ' ',regex=True)
fips = pd.read_csv('../../help-files/ak_jurisdiction_crosswalk.csv')
fips = fips.rename(columns={"precinct": "precinct_dummy"})
fips = fips.applymap(str)
df=df.applymap(lambda x: x.strip() if type(x)==str else x)
df = pd.merge(df, fips, on = ['precinct_dummy'],
how = 'left')
df = df.drop(['precinct_dummy'], axis=1)
df.loc[df['candidate'].str.contains('Number of Precincts for Race'), 'office'] = 'Number of Precincts for Race'
df.loc[df['candidate'].str.contains('Number of Precincts'), 'office'] = 'Number of Precincts'
df.loc[df['candidate'].str.contains('Number of Precincts Reporting'), 'office'] = 'Number of Precincts Reporting'
df.loc[df['candidate'].str.contains('Registered Voters'), 'office'] = 'Registered Voters'
df.loc[df['candidate'].str.contains('Times Counted'), 'office'] = 'Times Counted'
df.loc[df['office'].str.contains('Number of Precincts for Race'), 'mode'] = 'TOTAL'
df.loc[df['office'].str.contains('Number of Precincts'), 'mode'] = 'TOTAL'
df.loc[df['office'].str.contains('Number of Precincts Reporting'), 'mode'] = 'TOTAL'
df.loc[df['office'].str.contains('Registered Voters'), 'mode'] = 'TOTAL'
df.loc[df['office'].str.contains('Times Counted'), 'mode'] = 'TOTAL'
df['candidate'] = df['candidate'].replace({'Number of Precincts for Race': "",
'Number of Precincts': "",
'Number of Precincts Reporting': "",
'Registered Voters': "",
'Times Counted': ""})
df['party_detailed'] = df['party_detailed'].replace({'NP': "",
'"': "",
'CON""': "CONSTITUTION",
'DEM""': "DEMOCRAT",
'ALI""': "ALLIANCE",
'GRN""': "GREEN",
'LIB""': "LIBERTARIAN",
'NOM""': "INDEPENDENT",
'REP""': "REPUBLICAN",
'AIP""': "ALASKA INDEPENDENCE"})
df['party_simplified'] = df['party_detailed']
df['party_simplified'] = df['party_detailed'].replace({"CONSTITUTION": "OTHER",
"ALLIANCE": "OTHER",
"GREEN": "OTHER",
"INDEPENDENT": "OTHER",
"ALASKA INDEPENDENCE": "OTHER"})
def cleanCandidate(x):
if "YES" in x or "NO" in x or "Number of Precincts" in x:
if '""' in x:
return x[:-2]
else:
return x
if '""' in x:
x = x[:-2]
if " /" in x:
index = x.index(" /")
x = x[:index]
if len(x) < 1:
x = ""
else:
return x.upper()
return x.upper()
df['candidate'] = df['candidate'].apply(cleanCandidate)
df['candidate'] = df['candidate'].str.replace('\s+', ' ',regex=True)
temp = df.candidate.str.split(expand=True,)
temp = temp.replace(np.nan, "", regex = True)
temp['candidateName'] = temp[1] + " " + temp[2] + " " + temp[3] + " " + temp[4] + " " + temp[0]
temp['candidateName'] = temp['candidateName'].str.replace('\s+', ' ',regex=True)
temp['candidateName'] = temp['candidateName'].str.strip()
temp['candidateName'] = temp['candidateName'].str.replace("\.", "",regex=True)
def cleanTempCand(x):
if "JOSEPH R JR BIDEN" in x:
return "JOSEPH R BIDEN JR"
elif "ROBERT H JR MYERS" in x:
return "ROBERT H MYERS JR"
elif "JAMES A SR CANITZ" in x:
return "JAMES A CANITZ SR"
elif 'LA FUENTE ROCQUE "ROCKY" DE' in x:
return 'ROCQUE "ROCKY" DE LA FUENTE'
elif 'IMHOF NATASHA A VON' in x:
return 'NATASHA VON IMHOF'
else:
return x.upper()
temp['candidateName'] = temp['candidateName'].apply(cleanTempCand)
df['candidate'] = temp['candidateName']
df.loc[(df['office'].str.contains('- [a-zA-Z]')), 'candidate'] = df['candidate'] + df['office']
def cleanCourtCand(x):
if "YES" in x and '-' in x:
# index = x.index(" -")
# x = 'YES' + x[index+2:-2].upper()
return x.split(' - ')[-1].strip('""').upper() + ' - YES'
elif 'NO' in x and '-' in x:
# index = x.index(" -")
# x = 'NO' + x[index+2:-2].upper()
return x.split(' - ')[-1].strip('""').upper() + ' - NO'
else:
return x
# return x
df['candidate'] = df['candidate'].apply(cleanCourtCand)
df.loc[df['candidate'].str.contains('GAVIN S CHRISTIANSEN'), 'party_simplified'] = 'LIBERTARIAN'
df.loc[df['candidate'].str.contains('GAVIN S CHRISTIANSEN'), 'party_detailed'] = 'LIBERTARIAN'
df.loc[df['candidate'].str.contains('SCOTT A KOHLHAAS'), 'party_simplified'] = 'LIBERTARIAN'
df.loc[df['candidate'].str.contains('SCOTT A KOHLHAAS'), 'party_detailed'] = 'LIBERTARIAN'
df.loc[df['candidate'].str.contains('WILLY KEPPEL'), 'party_detailed'] = "VETERAN'S PARTY"
df['district'] = df['precinct']
df.loc[df['office'].str.contains('House District'), 'district'] = df['office']
df.loc[df['office'].str.contains('Senate District'), 'district'] = df['office']
df.loc[df['office'].str.contains('Superior Court'), 'district'] = df['office']
df.loc[df['office'].str.contains('District Court'), 'district'] = df['office']
df.loc[df['office'].str.contains('Number of Precincts Reporting'), 'district'] = ''
df.loc[df['office'].str.contains('Number of Precincts'), 'district'] = ''
df.loc[df['office'].str.contains('Registered Voters'), 'district'] = ''
df.loc[df['office'].str.contains('Times Counted'), 'district'] = ''
def getDistrict(x):
if '""' in x:
x = x[:-2]
if 'House District' in x:
x = x[-2:]
if 'Senate District' in x:
x = x[-1:]
if 'Superior Court' in x or 'District Court' in x:
index = x.index('Court')
x = x[index+6:index+9]
if ' - ' in x and 'Court' not in x:
index = x.index("District ")
return x[index+8:index+11]
elif 'HD99 Fed Overseas Absentee' in x:
return 'HD99 FED OVERSEAS ABSENTEE'
else:
return x
return x
df['district'] = df['district'].apply(getDistrict)
df["district"]= df["district"].str.strip()
df["district"]= df["district"].astype(str)
df.loc[df['district'].str.contains('HD99 FED OVERSEAS ABSENTEE'), 'district'] = ""
def padDistrict(x):
if len(x) < 3:
if x not in ['B', 'D', 'F', 'H', 'J', 'L', 'M', 'N', 'P', 'R', 'T', '']:
x = x.zfill(3)
return x
else:
return(x)
elif '-' in x:
return x[:6]
else:
return(x)
df['district'] = df['district'].apply(padDistrict)
df["district"]= df["district"].str.strip()
def cleanOffice(x):
if '""' in x:
x = x[:-2]
if ('-' in x) and ('Ballot Measure' not in x):
index = x.index(" -")
x = x[:index]
if "President" in x:
return "US PRESIDENT"
if "Senator" in x:
return "US SENATE"
if "Representative" in x:
return "US HOUSE"
if "Superior Court" in x:
return "SUPERIOR COURT"
if "District Court" in x:
return "DISTRICT COURT"
if "Senate District" in x:
return "STATE SENATE"
if "House District" in x:
return "STATE HOUSE"
else:
return x.upper()
return x.upper()
df['office'] = df['office'].apply(cleanOffice)
df.loc[df['office'].str.contains('US HOUSE'), 'district'] = "000"
df.loc[df['office'].str.contains('US PRESIDENT'), 'district'] = "STATEWIDE"
df.loc[df['office'].str.contains('US SENATE'), 'district'] = "STATEWIDE"
df.loc[df['office'].str.contains('BALLOT MEASURE NO.'), 'district'] = "STATEWIDE"
df.loc[df['office'].str.contains('SUPREME COURT'), 'district'] = "STATEWIDE"
df.loc[df['office'].str.contains('COURT OF APPEALS'), 'district'] = "STATEWIDE"
def getDataverse(x):
if 'NUMBER' in x or 'REGISTERED VOTERS' in x or 'TIMES COUNTED' in x or 'RACE STATISTICS' in x:
return ""
if 'US PRESIDENT' in x:
return 'PRESIDENT'
if "US SENATE" in x:
return 'SENATE'
if "US HOUSE" in x:
return 'HOUSE'
if "DISTRICT COURT" in x:
return "LOCAL"
if x in ['STATE HOUSE', 'SUPREME COURT', 'COURT OF APPEALS', 'SUPERIOR COURT',
'BALLOT MEASURE NO. 1 - 19OGTX', 'BALLOT MEASURE NO. 2 - 19AKBE', 'STATE SENATE']:
return 'STATE'
else:
return ""
df['dataverse'] = df['office'].apply(getDataverse)
df = df[df.office != 'RACE STATISTICS']
# Readme Check - No issues
df["readme_check"] = "FALSE"
df['precinct'] = df['precinct'].str.upper()
# drop number of precincts reporting info (complete reporting in file)
df = df[~(df['office'].str.contains('NUMBER OF PRECINCTS|TIMES COUNTED'))].drop_duplicates().copy()
# Final step: Remove all trailing white space and put columns in correct order.
df=df.applymap(lambda x: x.strip() if type(x)==str else x)
df=df[["precinct", "office", "party_detailed", "party_simplified", "mode", "votes", "county_name", "county_fips", "jurisdiction_name",
"jurisdiction_fips", "candidate", "district", "dataverse", "year", "stage", "state", "special", "writein", "state_po",
"state_fips", "state_cen", "state_ic", "date", "readme_check", "magnitude"]]
# df=df.set_index('precinct')
df.to_csv('2020-ak-precinct-general.csv',index=False, quoting=csv.QUOTE_NONNUMERIC)