forked from jseabold/538model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
campaign_finance.py
57 lines (48 loc) · 1.88 KB
/
campaign_finance.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
"""
Aggregate contributions
"""
import zipfile
import pandas
fin = zipfile.ZipFile("/home/skipper/school/seaboldgit/talks/pydata/data/fec_obama_itemized_indiv.zip")
fin = fin.open(fin.filelist[0].filename)
obama = pandas.read_csv(fin, skiprows=7)
obama = obama[["State", "Amount", "City", "Zip"]]
obama.Amount = obama.Amount.str.replace("\$", "").astype(float)
obama.State = obama.State.replace({"PE" : "PA"}) # typo
#AE, AA, AP, ZZ are armed forces or canada, etc.
#AB is Canada
state_contrib = obama.groupby("State")["Amount"].sum()
state_contrib = state_contrib.drop(["AE", "AA", "AP", "AB",
"AS", # American Samoa
"BC",
"BR",
"FM", # Micronesia
"GU", #Guam
"MP", #Marianas Islands
"NO",
"ON",
"PR",
"QU",
"SA",
"ZZ",
"VI",
])
state_contrib.to_csv("data/obama_indiv_state.csv")
fin = zipfile.ZipFile("/home/skipper/school/seaboldgit/talks/pydata/data/fec_romney_itemized_indiv.zip")
fin = fin.open(fin.filelist[0].filename)
romney = pandas.read_csv(fin, skiprows=7)
romney = romney[["State", "Amount", "City", "Zip"]]
romney.Amount = romney.Amount.str.replace("\$", "").astype(float)
romney.State = romney.State.replace({"TE" : "PN", "GE" : "GA",
"PN" : "TN", "HA" : "HI"}) # typo or outdated
state_contrib = romney.groupby("State")["Amount"].sum()
state_contrib = state_contrib.drop(["AE", "AA", "AP",
"AS", # American Samoa
"GU", #Guam
"MP", #Marianas Islands
"PR",
"VI",
"XX",
"FF"
])
state_contrib.to_csv("data/romney_indiv_state.csv")