-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaz2020.py
186 lines (143 loc) · 5.34 KB
/
az2020.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
import pandas as pd
import csv
raw = pd.read_csv('AZ_precinct_v2.csv')
def fix_precinct(precinct):
fixed = precinct.upper()
fixed = fixed.strip()
return fixed
def fix_office(office):
if 'U.S. REPRESENTATIVE' in office: return 'US HOUSE'
elif 'STATE SENATOR' in office: return 'STATE SENATE'
elif 'STATE REPRESENTATIVE' in office: return 'STATE HOUSE'
elif 'U.S. SENATOR' in office: return 'US SENATE'
elif 'PRESIDENT' in office: return 'US PRESIDENT'
elif 'RETENTION' in office and 'DIVISION' in office: return 'RETENTION COURT OF APPEALS'
else: return office
def fix_party_detailed(name):
string = str(name).upper()
if string == 'DEM': return 'DEMOCRAT'
elif string == 'REP': return 'REPUBLICAN'
elif string == 'NAN': return 'NONPARTISAN'
elif string == 'NONE': return ""
elif string == 'IRP': return 'INDEPENDENT REPUBLICAN PARTY'
elif string == 'LBT': return 'LIBERTARIAN'
elif string == 'IND': return 'INDEPENDENT'
elif string == 'ACN': return 'CONSTITUTION'
elif string == 'CSM': return 'COMMON SENSE MODERATE'
elif string == 'UPA': return 'UNITY'
elif string == 'GREEN': return "GREEN"
elif string == 'PARTY FOR SOCIALISM AND LIBERATION': return "PARTY FOR SOCIALISM AND LIBERATION"
else: return string
def fix_party_simplified(detailed):
if detailed == 'REPUBLICAN': return 'REPUBLICAN'
elif detailed == 'DEMOCRAT': return 'DEMOCRAT'
elif detailed == 'LIBERTARIAN': return 'LIBERTARIAN'
elif detailed == 'NONPARTISAN': return 'NONPARTISAN'
elif detailed == '': return ""
else: return 'OTHER'
def fix_candidate(name):
title = name.replace('.', '')
title = title.replace('""', '"') #seth ""marcus"" sifuentes
title = title.replace(' ', ' ')
title = title.strip()
title = title.replace('É', "E")
title = title.replace('Í', "I")
title = title.replace('Á', "A")
title = title.replace('Ú', "U")
title = title.replace('Ó', "O")
title = title.replace('Ñ', "N")
if title == 'KIMBERLY "KIM" BEACH - MOSCHETTI':
return 'KIMBERLY "KIM" BEACH-MOSCHETTI'
return title.upper()
def get_div(title):
if 'DIVISION II' in title:
if 'OTHER' in title:
return '002, OTHER'
elif 'PIMA' in title:
return '002, PIMA'
elif 'DIVISION I' in title:
if 'OTHER' in title:
return '001, OTHER'
elif 'MARICOPA' in title:
return '001, MARICOPA'
return title
def fix_district(dist):
title = dist.upper()
title = title.strip()
if 'DISTRICT' in title:
num = title[-2:]
if num[0] == ' ':
return '00' + num[1]
return '0' + num
if title == 'ARIZONA SUPREME COURT':
return 'STATEWIDE'
elif 'DIVISION' in title:
return get_div(title)
return title
def get_dataverse(office):
if office == 'US HOUSE': return 'HOUSE'
elif office == 'US SENATE': return 'SENATE'
elif office == 'US PRESIDENT': return 'PRESIDENT'
else: return 'STATE'
def fix_special(office):
if office == 'US SENATE': return 'TRUE'
return 'FALSE'
def fix_writein(name):
if str(name) == 'True': return 'TRUE'
return 'FALSE'
def fix_stage(string):
return string.upper()
def fix_magnitude(magnitude):
if str(magnitude) == 'nan':
return 1
return int(magnitude)
def fix_mode(mode):
if mode == 'EARLY BALLOTS': return 'EARLY'
elif mode == 'POLLING PLACE': return 'ELECTION DAY'
elif mode == 'PROVISIONAL BALLOTS': return 'PROVISIONAL'
return mode
fips = {
'MARICOPA': '04013',
'PINAL': '04021',
'YUMA': '04027',
'COCONINO': '04005',
'COCHISE': '04003',
'MOHAVE': '04015',
'GRAHAM': '04009',
'SANTA CRUZ': '04023',
'APACHE': '04001',
'LA PAZ': '04012',
'YAVAPAI': '04025',
'NAVAJO': '04017',
'GREENLEE': '04011',
'PIMA': '04019',
'GILA': '04007'
}
def fix_fips(county):
return fips[county]
raw['precinct'] = raw['precinct'].apply(fix_precinct)
raw['office'] = raw['office'].apply(fix_office)
raw['party_detailed'] = raw['party_detailed'].apply(fix_party_detailed)
raw['party_simplified'] = raw['party_detailed'].apply(fix_party_simplified)
raw['candidate'] = raw['candidate'].apply(fix_candidate)
raw['district'] = raw['district'].apply(fix_district)
raw['district'] = raw['district'].astype(str)
raw['dataverse'] = raw['office'].apply(get_dataverse)
raw['stage'] = raw['stage'].apply(fix_stage)
raw['magnitude'] = raw['magnitude'].apply(fix_magnitude)
raw['date'] = '2020-11-03' ### fix date format
raw['mode'] = raw['mode'].apply(fix_mode)
raw['special'] = raw['office'].apply(fix_special)
raw['special'] = raw['special'].astype(str)
raw['writein'] = raw['writein'].apply(fix_writein)
raw['special'] = raw['special'].astype(str)
raw['writein'] = raw['writein'].astype(str)
raw['county_fips'] = raw['county_name'].apply(fix_fips)
raw['jurisdiction_fips'] = raw['county_fips']
raw['county_fips'] = raw['county_fips'].astype(str)
raw['jurisdiction_fips'] = raw['jurisdiction_fips'].astype(str)
raw['readme_check'] = 'FALSE'
raw['readme_check'] = raw['readme_check'].astype(str)
# temporarily removing writein candidates with 0 votes
raw = raw[~((raw['writein']=='TRUE')&(raw['votes']==0))]
raw.to_csv('2020-az-precinct-general.csv', index = None, quoting=csv.QUOTE_NONNUMERIC )