-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathak2018.py
More file actions
211 lines (151 loc) · 6.28 KB
/
ak2018.py
File metadata and controls
211 lines (151 loc) · 6.28 KB
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
#!/usr/bin/env python
# coding: utf-8
# It appears Alyse Galvin—a house candidate in Alaska—is coded as democrat in district_overall_2018.csv, when according to her campaign website and ballotpedia.org page she ran as an independent.
#
# Not sure if there are any other races miscoded in a such a way, but should presumably be looked into.
#
#
# ~district~, candidate, ~office~, ~readme_check~, ~magnitude~, ~special~, ~precinct~, ~dataverse~, ~mode~, ~votes~, ~party_detailed~, ~party_simplified~, ~county_name~ , ~county_fips~, ~jurisdiction_name~, ~jurisdiction_fips~, ~year~, ~stage~, ~state~, ~writein~, ~state_po~, ~state_fips~, ~state_cen~, ~state_ic~, ~date~.
# In[6]:
import pandas as pd
import numpy as np
import os
import csv
df = pd.read_csv('2018-ak-precinct-autoadapted.csv')
# In[10]:
pd.set_option('display.max_columns', None)
df
# In[8]:
df["date"] = "2018-11-06"
df["readme_check"] = "FALSE"
df["magnitude"] = 1
df = df.drop(['jurisdiction_fips'], axis=1)
df['county_name'] = ""
df['county_fips'] = ""
# In[9]:
fips = pd.read_csv('/Users/declanchin/Desktop/MEDSL/2020-precincts/help-files/ak_jurisdiction_crosswalk.csv')
fips = fips.applymap(str)
fips['precinct'] = fips['precinct'].str.upper()
df=df.applymap(lambda x: x.strip() if type(x)==str else x)
df = pd.merge(df, fips, on = ['precinct', 'jurisdiction_name'],
how = 'left')
df['jurisdiction_fips'] = df['jurisdiction_fips'].str.zfill(5).replace('00000','')
# In[11]:
# NOTE: Will be empty for AK (from 2020-readme)
# df['county_name'] = df['jurisdiction_name']
# df['county_fips'] = df['jurisdiction_fips']
# In[12]:
df.district = df.district.str.strip()
df["district"]= df["district"].str.zfill(3)
# df.district = df.district.str.replace('000','', regex=False) #this is for US HOUSE
df.district = df.district.str.replace('00A','A', regex=False)
df.district = df.district.str.replace('00C','C', regex=False)
df.district = df.district.str.replace('00E','E', regex=False)
df.district = df.district.str.replace('00G','G', regex=False)
df.district = df.district.str.replace('00I','I', regex=False)
df.district = df.district.str.replace('00K','K', regex=False)
df.district = df.district.str.replace('00M','M', regex=False)
df.district = df.district.str.replace('00O','O', regex=False)
df.district = df.district.str.replace('00Q','Q', regex=False)
df.district = df.district.str.replace('00S','S', regex=False)
# In[16]:
# for x in df['candidate'].unique():
# print(x)
# In[17]:
df['candidate'] = df['candidate'].replace(np.nan, "")
df['candidate'] = df['candidate'].astype(str)
def cleanCandidate(x):
if ',' in x:
index = x.index(',')
x = (x[index+2:] + ' ' + x[:index])
if '.' in x:
x = x.replace('.', '')
if 'BEGICH/CALL' in x:
x = 'MARK BEGICH'
if 'DUNLEAVY/MEYER' in x:
x = 'MIKE DUNLEAVY'
if 'TOIEN/CLIFT' in x:
x = 'WILLIAM TOIEN'
if 'WALKER/MALLOTT' in x:
x = 'BILL WALKER'
if 'LYLE' in x:
index = x.index('|')
x = 'PAUL R LYLE - ' + x[index+1:]
if 'MCCONAHY' in x:
index = x.index('|')
x = 'MICHAEL P MCCONAHY - ' + x[index+1:]
if 'SEEKINS' in x:
index = x.index('|')
x = 'BEN SEEKINS - ' + x[index+1:]
if 'COREY' in x:
index = x.index('|')
x = 'MICHAEL COREY - ' + x[index+1:]
if 'MORSE' in x:
index = x.index('|')
x = 'WILLIAM F MORSE - ' + x[index+1:]
if 'WALKER JR.' in x:
index = x.index('|')
x = 'HERMAN G WALKER JR - ' + x[index+1:]
if 'WOLVERTON' in x:
index = x.index('|')
x = 'MICHAEL L WOLVERTON - ' + x[index+1:]
if 'CHUNG' in x:
index = x.index('|')
x = 'JO-ANN M CHUNG - ' + x[index+1:]
if 'CLARK' in x:
index = x.index('|')
x = 'BRIAN K CLARK - ' + x[index+1:]
if 'ESTELLE' in x:
index = x.index('|')
x = 'WILLIAM L ESTELLE - ' + x[index+1:]
if 'ILLSLEY' in x:
index = x.index('|')
x = 'SHARON A S ILLSLEY - ' + x[index+1:]
if 'WOLFE' in x:
index = x.index('|')
x = 'JOHN W WOLFE - ' + x[index+1:]
if 'CAREY' in x:
index = x.index('|')
x = 'WILLIAM B CAREY - ' + x[index+1:]
if 'MILLER' in x:
index = x.index('|')
x = 'GREGORY MILLER - ' + x[index+1:]
if 'SWANSON' in x:
index = x.index('|')
x = 'KRISTEN SWANSON - ' + x[index+1:]
if 'KATELNIKOFF-LESTER' in x:
x = 'SANDRA KATELNIKOFF-LESTER'
else:
return x.upper()
return x.upper()
# In[18]:
df['candidate'] = df['candidate'].apply(cleanCandidate)
df['candidate'] = df['candidate'].str.replace('\s+', ' ',regex=True)
# In[76]:
df['candidate'] = df['candidate'].replace(['WALKER JR|YES','WALKER JR|NO'],
['HERMAN G WALKER JR - YES','HERMAN G WALKER JR - NO'])
# In[84]:
df.loc[df['candidate'].str.contains('ALYSE S GALVIN'), 'party_detailed'] = "INDEPENDENT"
df.loc[df['candidate'].str.contains('NINA AHMAD') & df['office'].str.contains('ATTORNEY GENERAL'), 'office'] = "AUDITOR GENERAL"
df['party_detailed'] = df['party_detailed'].fillna("")
def get_party_simplified(x):
if x in ["",'DEMOCRAT','REPUBLICAN',"LIBERTARIAN",'NONPARTISAN']:
return x
else:
return "OTHER"
df['party_simplified'] = df['party_detailed'].apply(get_party_simplified)
#Readme Check - Setting as FALSE for now
df["readme_check"] = "FALSE"
df = df[~((df.drop(columns = 'votes').duplicated(keep = False)) & (df['votes']==0))].copy()
# Final step: Remove all trailing white space and put columns in correct order.
# dc addition, change candidate blank to writein
df = df.replace([True,False], ['TRUE','FALSE'])
df.loc[((df['candidate']=="")&(df['writein']=='TRUE')),'candidate'] = 'WRITEIN'
df=df.replace(' ', ' ', regex = True)
df = df.replace(np.nan, "")
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"]]
# In[80]:
df.to_csv('2018-ak-precinct-general-updated.csv', quoting=csv.QUOTE_NONNUMERIC,index=False)