-
Notifications
You must be signed in to change notification settings - Fork 1
/
florida_vf.py
398 lines (356 loc) · 10.6 KB
/
florida_vf.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""
This module contains functions for creating a pandas dataframe version of the
florida voter registration list. The florida voter file is available upon request
from -- https://dos.myflorida.com/elections/data-statistics/voter-registration-statistics/voter-extract-disk-request/
the voter file fields are included in 'state_voter_files/florida/voterfile_cols.txt' which
is a manually created text file from the documentation of the voter file. in florida, there
is one voter file per county and all county files are saved in the directory
'state_voter_files/florida/20201208_VoterDetail'
Functions
---------
fl_voter_file
pandas dataframe version of the voter file
"""
import pandas as pd
import os
# the race/ethnicity categories used by the florida voter file
VOTER_RACES_STR = {
"1": "nh_aian",
"2": "nh_api",
"3": "nh_black",
"4": "hispanic",
"5": "nh_white",
"6": "other",
"7": "multi-racial",
"9": "unknown",
}
COUNTY_DICT = {
"ALA": "Alachua",
"BAK": "Baker",
"BAY": "Bay",
"BRA": "Bradford",
"BRE": "Brevard",
"BRO": "Broward",
"CAL": "Calhoun",
"CHA": "Charlotte",
"CIT": "Citrus",
"CLA": "Clay",
"CLL": "Collier",
"CLM": "Columbia",
"DAD": "Miami-Dade",
"DES": "Desoto",
"DIX": "Dixie",
"DUV": "Duval",
"ESC": "Escambia",
"FLA": "Flagler",
"FRA": "Franklin",
"GAD": "Gadsden",
"GIL": "Gilchrist",
"GLA": "Glades",
"GUL": "Gulf",
"HAM": "Hamilton",
"HAR": "Hardee",
"HEN": "Hendry",
"HER": "Hernando",
"HIG": "Highlands",
"HIL": "Hillsborough",
"HOL": "Holmes",
"IND": "Indian River",
"JAC": "Jackson",
"JEF": "Jefferson",
"LAF": "Lafayette",
"LAK": "Lake",
"LEE": "Lee",
"LEO": "Leon",
"LEV": "Levy",
"LIB": "Liberty",
"MAD": "Madison",
"MAN": "Manatee",
"MRN": "Marion",
"MRT": "Martin",
"MON": "Monroe",
"NAS": "Nassau",
"OKA": "Okaloosa",
"OKE": "Okeechobee",
"ORA": "Orange",
"OSC": "Osceola",
"PAL": "PalmBeach",
"PAS": "Pasco",
"PIN": "Pinellas",
"POL": "Polk",
"PUT": "Putnam",
"SAN": "SantaRosa",
"SAR": "Sarasota",
"SEM": "Seminole",
"STJ": "St.Johns",
"STL": "St.Lucie",
"SUM": "Sumter",
"SUW": "Suwannee",
"TAY": "Taylor",
"UNI": "Union",
"VOL": "Volusia",
"WAK": "Wakulla",
"WAL": "Walton",
"WAS": "Washington",
}
FL_COUNTY_REGIONS_DICT = {
"northwest": [
"Escambia",
"SantaRosa",
"Okaloosa",
"Walton",
"Holmes",
"Washington",
"Bay",
"Jackson",
"Calhoun",
"Gulf",
"Franklin",
"Liberty",
],
"northcentral": [
"Gadsden",
"Leon",
"Wakulla",
"Jefferson",
"Madison",
"Taylor",
"Dixie",
"Lafayette",
"Suwannee",
"Hamilton",
"Columbia",
"Gilchrist",
"Levy",
"Alachua",
"Bradford",
"Union",
],
"northeast": ["Baker", "Nassau", "Duval", "Clay", "St.Johns", "Putnam", "Flagler"],
"centralwest": [
"Citrus",
"Hernando",
"Pasco",
"Hillsborough",
"Pinellas",
"Manatee",
"Sarasota",
"Desoto",
],
"central": [
"Marion",
"Sumter",
"Lake",
"Orange",
"Seminole",
"Osceola",
"Polk",
"Hardee",
"Highlands",
],
"centraleast": ["Volusia", "Brevard", "Indian River", "Okeechobee", "St.Lucie"],
"southwest": ["Glades", "Charlotte", "Lee", "Hendry", "Collier"],
"southeast": ["Martin", "PalmBeach", "Broward", "Monroe", "Miami-Dade"],
}
FL_RURAL_COUNTIES = [
'Walton',
'Holmes',
'Washington',
'Jackson',
'Calhoun',
'Gulf',
'Gadsden',
'Liberty',
'Franklin',
'Wakulla',
'Jefferson',
'Madison',
'Taylor',
'Hamilton',
'Suwannee',
'Lafayette',
'Dixie',
'Columbia',
'Gilchrist',
'Levy',
'Baker',
'Union',
'Bradford',
'Hardee',
'Desoto',
'Highlands',
'Okeechobee',
'Glades',
'Hendry',
'Monroe'
]
COUNTIES_POP_ORDER = ['Lafayette', 'Liberty', 'Glades', 'Union', 'Hamilton', 'Calhoun',
'Franklin', 'Jefferson', 'Gulf', 'Dixie', 'Holmes', 'Madison',
'Gilchrist', 'Taylor', 'Hardee', 'Baker', 'Washington', 'Bradford',
'Desoto', 'Hendry', 'Okeechobee', 'Wakulla', 'Suwannee', 'Jackson',
'Levy', 'Gadsden', 'Columbia', 'Putnam', 'Monroe', 'Walton',
'Highlands', 'Nassau', 'Flagler', 'Sumter', 'Citrus', 'Martin',
'Indian River', 'Bay', 'Hernando', 'SantaRosa', 'Okaloosa', 'Charlotte',
'Clay', 'Alachua', 'St.Johns', 'Leon', 'St.Lucie', 'Escambia',
'Collier', 'Osceola', 'Lake', 'Marion', 'Manatee', 'Seminole',
'Sarasota', 'Pasco', 'Volusia', 'Brevard', 'Polk', 'Lee', 'Duval',
'Pinellas', 'Orange', 'Hillsborough', 'PalmBeach', 'Broward',
'Miami-Dade']
COUNTY_TO_REGION_DICT = {}
for key in FL_COUNTY_REGIONS_DICT:
for county in FL_COUNTY_REGIONS_DICT[key]:
COUNTY_TO_REGION_DICT[county] = key
def main():
df_vf = fl_voter_file(verbose=True, save=True, load=False)
quit()
df_vf['county_long'] = df_vf['county'].map(COUNTY_DICT)
df_vf['rural'] = df_vf['county_long'].isin(FL_RURAL_COUNTIES)
print(df_vf.head())
rural_counties2 = df_vf[df_vf['rural']]['county_long'].unique()
rural_counties2.sort()
print(rural_counties2)
FL_RURAL_COUNTIES.sort()
print(FL_RURAL_COUNTIES)
def fl_voter_file(min_names=0, verbose=False, load=True, save=True):
"""
first reads from a file the information about the format
of Florida's detailed voter file (column names and number of characters
in each column). Then, using that information, convert the voter file
to a pandas dataframe.
Parameters
----------
min_names : int, optional
exclude names that appear fewer than this many times
verbose : bool, optional
verbose
load : bool, optional
load the dataframe if it already exists
save : bool, optional
save the dataframe after creating it
Returns
-------
pandas dataframe
voter file
"""
# if this dataframe has already been created, read and return it
file_out = f"generated_data/florida_vf.feather"
if os.path.exists(file_out) and load:
print(f"*loading dataframe from {file_out}")
return pd.read_feather(file_out)
# read in file with with information on columns of voterfile data
filename = "state_voter_files/florida/voterfile_cols.txt"
df_vf_format = pd.read_csv(
filename, sep="\t", index_col=0, header=None, names=["name", "chars", "notes"]
)
# if the dataframe hasn't yet been created, then create it
# names of columns to use
usecols = [
"County Code",
"Name Last ",
"Residence Address Line 1",
"Residence City (USPS)",
"Residence Zipcode",
"Gender",
"Race",
"Party Affiliation",
"Voter Status",
]
# more convenient column names
renamed_cols = [
"county",
"name",
"address",
"city",
"zipcode",
"gender",
"race",
"party",
"voter_status",
]
# the florida voter file consists of separate .txt files for each
# county. go through to each county and read in the data
dfs = []
for filename in os.listdir("state_voter_files/florida/20201208_VoterDetail"):
if filename[-12:] != "20201208.txt":
continue
fn = f"state_voter_files/florida/20201208_VoterDetail/{filename}"
if verbose:
print(f"reading {fn}")
df = pd.read_csv(
fn,
sep="\t",
header=None,
dtype=str,
names=df_vf_format["name"],
usecols=usecols,
)
df.columns = renamed_cols
dfs.append(df)
df = pd.concat(dfs)
# make all names lowercase
df["name"] = df["name"].str.lower()
# change race/ethnicity convention
df["race"].replace(VOTER_RACES_STR, inplace=True)
df = filter_vf(df, min_names=min_names, verbose=verbose)
# add region
df["region"] = df["county"].map(lambda x: COUNTY_DICT.get(x, x))
df["region"] = df["region"].map(lambda x: COUNTY_TO_REGION_DICT.get(x, x))
# add rural or urban designation
df['county_long'] = df['county'].map(COUNTY_DICT)
df['rural'] = df['county_long'].isin(FL_RURAL_COUNTIES)
print(df)
# write dataframe to feather, but first reset index as required by feather
if save:
df = df.reset_index(drop=True)
df.to_feather(file_out)
return df
def filter_vf(df_vf0, min_names=0, verbose=False):
"""
the 12.2020 voter file has 15mil entries and 740K unique names,
but some rows need cleaning
Parameters
----------
df_vf0 : pandas dataframe
uncleaned voter file
min_names : int, optional
exclude names that appear fewer than this many times
Returns
-------
pandas dataframe
cleaned voter file
"""
# first make a copy
df_vf = df_vf0.copy()
if verbose:
print(f"fl voter file total entries: {df_vf0.shape[0]}")
# remove those with missing and redacted names (245 rows)
df_vf = df_vf[~df_vf["name"].isnull()]
df_vf = df_vf.loc[df_vf["name"] != "*", :]
if verbose:
print(f"after removing missing names: {df_vf.shape[0]} entries")
# filter out people of unknown races
df_vf = df_vf.loc[df_vf["race"] != "unknown", :]
if verbose:
print(f"after removing unknown races: {df_vf.shape[0]} entries")
# set multi-racial to "other"
df_vf.loc[df_vf["race"] == "multi-racial", "race"] = "other"
# filter out names that don't appear frequently enough (~1mil)
df_tmp = df_vf0.groupby(["name"]).size()
df_tmp = df_tmp.to_frame("count").reset_index()
df_tmp = df_tmp.loc[(df_tmp["count"] > min_names), :]
names = df_tmp["name"].unique()
df_vf = df_vf.loc[df_vf["name"].isin(names), :]
# only active registrations
df_vf = df_vf.loc[(df_vf["voter_status"] == "ACT"), :]
if verbose:
print(f"after removing inactive registrations: {df_vf.shape[0]} entries")
# filter out any names with numbers or any names with no letters
# does name contain a digit
mask1 = df_vf["name"].map(lambda x: any(char.isdigit() for char in str(x)))
# does name contain zero letters
mask2 = ~df_vf["name"].map(lambda x: any(char.isalpha() for char in str(x)))
mask = ~mask1 & ~mask2
df_vf = df_vf[mask]
return df_vf
if __name__ == "__main__":
main()