-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtablescraper.py
More file actions
54 lines (44 loc) · 1.71 KB
/
tablescraper.py
File metadata and controls
54 lines (44 loc) · 1.71 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pandas as pd
import wikipedia as wp
html = "https://en.wikipedia.org/wiki/List_of_United_States_presidential_election_results_by_state"
df = pd.read_html(html, header=0)[0]
df.drop(df.index[range(52,98)], inplace=True)
states = df['State']
debateyears = ["1960", "1976", "1980", "1984", "1988", "1992", "1996", "2000 ‡", "2004", "2008", "2012", "2016 ‡"]
wins = ["D", "D", "R", "R", "R", "D", "D", "R", "R", "D", "D", "R"]
swingstates = ["Florida", "Iowa", "Michigan", "Minnesota", "Nevada", "New Hampshire", "North Carolina", "Ohio", "Pennsylvania", "Virginia", "Wisconsin"]
for year, win in zip(debateyears, wins):
Dem = 0
Rep = 0
Dsstates = 0
Rsstates = 0
for i in range(52):
Rhit = 0
Dhit = 0
if df[year][i] == "D":
Dem+=1
Dhit=1
elif df[year][i] == "R":
Rep+=1
Rhit=1
for sstate in swingstates:
if states[i] == sstate and Dhit == 1:
Dsstates+=1
elif states[i] == sstate and Rhit == 1:
Rsstates+=1
print(year, "\n\nDemocrat Won States:", Dem, "Republican Won States: ", Rep, "\nDemocrat Won Swing States:", Dsstates, "Republican Won Swing States: ", Rsstates)
if win == "D":
print("Democrat Won Election\n")
elif win == "R":
print("Republican Won Election\n")
for i, state in zip(range(52), states):
Dem = 0
Rep = 0
for year in debateyears:
if df[year][i] == "D":
Dem+=1
Dhit=1
elif df[year][i] == "R":
Rep+=1
Rhit=1
print(state, "\nTimes Voted Democrat:", Dem, "Times Votes Republican: ", Rep, "\n")