-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincumbency_analysis.py
More file actions
35 lines (32 loc) · 1.1 KB
/
incumbency_analysis.py
File metadata and controls
35 lines (32 loc) · 1.1 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
import pandas as pd
import string
data = pd.read_csv("output.csv")
incumbents = {}
adding_now = {}
output = []
punc_translator = str.maketrans('', '', string.punctuation)
space_translator = str.maketrans('', '', string.whitespace)
for i in range(1, 40):
for index, row in data[data["congress"] == i].iterrows():
name_processed = str(row["name"]).translate(punc_translator)
name_processed = name_processed.translate(space_translator)
data_tuple = (
row["state"],
row["district"],
name_processed,
)
if data_tuple in incumbents:
row["incumbent"] = True
row["old_vote_share"] = incumbents[data_tuple]
row["clean_name"] = name_processed
else:
row["incumbent"] = False
row["old_vote_share"] = None
row["clean_name"] = name_processed
if row["result"] == "won":
adding_now[data_tuple] = row["percentage"]
output.append(row)
incumbents = adding_now
adding_now = {}
output = pd.DataFrame(output)
output.to_csv("output_with_incumbency.csv")