-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathva2020.py
235 lines (185 loc) · 8.24 KB
/
va2020.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 6 11:29:04 2021
@author: jennifermiranda
"""
import pandas as pd
import numpy as np
import csv
df = pd.read_csv('2020 November General.csv', delimiter=",")
df = df.replace(np.nan, '', regex = True)
# Creating formatted 'candidate' column from name columns and removing whitespace:
df['candidate'] = df["FirstName"] + " " + df["MiddleName"] + " " + df["LastName"] + " " + df["Suffix"]
df['candidate'] = df['candidate'].str.replace('[^\w\s]','',regex=True)
df['candidate'] = df['candidate'].str.upper()
df['candidate'] = df['candidate'].str.replace('WRITE IN VOTES', 'WRITEIN')
df['candidate'] = df['candidate'].str.replace('\s+', ' ',regex=True)
# Make column for write in votes:
df['writein'] = df["LastName"].str.contains('write in votes', case=False)
# Drop unnecessary columns:
df = df.drop(['FirstName', 'MiddleName', 'LastName', 'WriteInVote',
'Suffix', 'ElectionName', 'DistrictType'], axis=1)
df = df.drop(['CandidateUid', 'LocalityUid', 'PrecinctUid', 'OfficeUid',
'ElectionUid', 'DistrictUid', 'LocalityCode'], axis=1)
# Rename columns and fix format of variables:
df = df.rename(columns={"PrecinctName": "precinct", "OfficeTitle": "office",
"Party": "party_detailed", "TOTAL_VOTES": "votes",
"LocalityName": "county_name", "ElectionType": "stage",
"ElectionDate": "date"})
# Adding 'date', 'stage', 'year', 'party_detailed' columns
df['date'] = '2020-11-03'
df['stage'] = 'GEN'
df['year'] = '2020'
df['party_detailed'] = df['party_detailed'].str.upper()
# 'special' column
df["special"] = df["office"].str.contains('Special')
df['special'] = df['special'].apply(str)
df['special'] = df['special'].str.upper()
# 'votes' columns; int to str
df['votes'] = df['votes'].replace({'' : 0})
df["votes"] = df["votes"].apply(np.int64)
df["votes"] = df["votes"].apply(str)
# 'writein' column
df['writein'] = df['writein'].apply(str)
df['writein'] = df['writein'].str.upper()
# 'mode' column; extracting mode from precinct column
df['mode'] = 'ELECTION DAY'
df.loc[df['precinct'].str.contains('Central Absentee Precinct'), 'mode'] = 'ABSENTEE'
df.loc[df['precinct'].str.contains('Provisional'), 'mode'] = 'PROVISIONAL'
def get_office(x):
"""
Extract the office name from the range name.
Parameters
---------
x: str
"office" in the VA data
Returns
-------
office: str,
The name of the office extracted from the race name
"""
if "United States Senate" in x:
return "US SENATE"
elif "President" in x:
return "US PRESIDENT"
elif "House of Representatives" in x:
return "US HOUSE"
elif "Mayor" in x:
return "MAYOR"
# elif "Town Council" in x:
# return "TOWN COUNCIL"
# elif "Member City Council" in x:
# return "CITY COUNCIL"
# elif "School Board" in x:
# return "SCHOOL BOARD"
elif "Member House of Delegates" in x:
return "STATE HOUSE"
# elif "Board of Supervisors" in x:
# return "BOARD OF SUPERVISORS"
# elif "County Board" in x:
# return "COUNTY BOARD"
else:
return x.upper()
df['office'] = df['office'].apply(get_office).str.upper()
df['party_detailed'] = df['party_detailed'].replace({"DEMOCRATIC" : "DEMOCRAT",
"WRITE-IN": "WRITEIN"})
df['party_simplified'] = df['party_detailed'].replace({"DEMOCRATIC" : "DEMOCRAT",
"WRITE-IN": "WRITEIN"})
df.loc[df['office'].str.contains('COUNCIL'), 'party_detailed'] = 'NONPARTISAN'
df.loc[df['office'].str.contains('COUNCIL'), 'party_simplified'] = 'NONPARTISAN'
df.loc[df['office'].str.contains('MAYOR'), 'party_detailed'] = 'NONPARTISAN'
df.loc[df['office'].str.contains('MAYOR'), 'party_simplified'] = 'NONPARTISAN'
df.loc[df['office'].str.contains('BOARD'), 'party_detailed'] = 'NONPARTISAN'
df.loc[df['office'].str.contains('BOARD'), 'party_simplified'] = 'NONPARTISAN'
df.loc[df['office'].str.contains('ATTORNEY'), 'party_detailed'] = 'NONPARTISAN'
df.loc[df['office'].str.contains('ATTORNEY'), 'party_simplified'] = 'NONPARTISAN'
df.loc[df['office'].str.contains('CLERK OF COURT'), 'party_detailed'] = 'NONPARTISAN'
df.loc[df['office'].str.contains('CLERK OF COURT'), 'party_simplified'] = 'NONPARTISAN'
def get_party_simp(x):
"""
Extract the simplified party name from the range name.
Parameters
---------
x: str
"party_detailed" in the data
Returns
-------
office: str,
The name of the simplified party from the race name
"""
if "DEMOCRAT" in x or "REPUBLICAN" in x or "LIBERTARIAN" in x or "NONPARTISAN" in x or "WRITEIN" in x:
return x
if "" == x:
return x
else:
return "OTHER"
df['party_simplified'] = df['party_simplified'].apply(get_party_simp)
df["county_name"] = df['county_name'].str.replace('COUNTY', '')
def get_district(x):
if 'DISTRICT' in x:
x = x.replace("DISTRICT", "").strip()
x = x.zfill(3)
elif '' == x:
return x
else:
x = x.zfill(3)
return x.upper()
df['DistrictName'] = df['DistrictName'].apply(str)
df['district'] = df['DistrictName'].apply(get_district)
df = df.drop(['DistrictName'], axis=1)
# Use merge_on_statecodes.csv to get info for VA:
df["state"] = 'VIRGINIA'
df["state_po"] = 'VA'
df["state_fips"] = '51'
df["state_cen"] = '54'
df["state_ic"] = '40'
# Make fips columns:
# After county name fix, append on fips codes
df["county_name"] = df['county_name'].str.replace('KING & QUEEN','KING AND QUEEN')
fips = pd.read_csv('../../help-files/county-fips-codes.csv')
fips['state'] = fips['state'].str.upper()
fips = fips.applymap(str)
df=df.applymap(lambda x: x.strip() if type(x)==str else x)
df = pd.merge(df, fips, on = ['state','county_name'],
how = 'left')
df['jurisdiction_name'] = df['county_name']
df['jurisdiction_fips'] = df['county_fips']
# Adding 'special' election for STATE HOUSE election in 2020
df.loc[(df['office'] == 'STATE HOUSE') & (df['district'] == '029'), 'special'] = 'TRUE'
def get_dataverse(x):
if 'PRESIDENT' in x:
return 'PRESIDENT'
elif "US SENATE" in x:
return "SENATE"
elif "US HOUSE" in x:
return "HOUSE"
elif x in ["STATE SENATE", "STATE HOUSE", "STATE SENATOR", "GOVERNOR",
"SECRETARY OF STATE", "ATTORNEY GENERAL",
"AUDITOR", "SUPERINTENDENT OF PUBLIC INSTRUCTION",
"COMMISSIONER OF AGRICULTURE", "COMMISSIONER OF LABOR",
"COMMISSIONER OF INSURANCE", "SUPREME COURT ASSOCIATE JUSTICE",
"SUPREME COURT CHIEF JUSTICE", "SUPERIOR COURT"]:
return "STATE"
else: # all others are local
return "LOCAL"
df['dataverse'] = df['office'].apply(get_dataverse)
# Readme Check - No issues
df["readme_check"] = "FALSE"
df['magnitude'] = 1
# 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"]]
official_dtypes = {'district':str,'precinct':str,'office':str, 'party_detailed':str,
'party_simplified':str,'mode':str,'votes':int, 'county_name':str, 'county_fips':str,
'jurisdiction_name':str,'jurisdiction_fips':str, 'candidate':str, 'district':str,
'dataverse':str,'year':int, 'stage':str, 'state':str, 'special':str, 'writein':str,
'state_po':str, 'state_fips':str, 'state_cen':str, 'state_ic':str, 'date':str,
'readme_check':str,'magnitude':int}
df = df.fillna("").astype(official_dtypes)
# removing a couple near duplicates with 0 votes
df=df[~(df.drop(columns = 'votes').duplicated(keep = False) & (df['votes']==0))].copy()
df['precinct'] = df['precinct'].str.upper()
df.to_csv('2020-va-precinct-general.csv',quoting=csv.QUOTE_NONNUMERIC, index=False)