-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.py
51 lines (40 loc) · 1.27 KB
/
merge.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
import os
import pandas as pd
states = {
"AS": 126,
"KL": 140,
"PY": 30,
"TN": 234,
"WB": 294
}
def get_code(party):
if party.lower() == "none of the above":
return "NOTA"
party = party.replace("of ", "") # handle CPIM
parts = party.split(" ")
parts = [p.strip() for p in parts]
return "".join(p[0] if not p.startswith("(") else p[0:2]+p[-1] for p in parts).upper()
def main():
for state in states:
print("Merging files of ", state)
base_dir = os.path.join("may2021", state)
df = None
for i in range(1, states[state] + 1):
filename = os.path.join(base_dir, f"{i}.csv")
try:
data = pd.read_csv(filename)
except FileNotFoundError:
print("Cannot find file: ", filename)
continue
data["AC_NO"] = i
data["Position"] = data["Total Votes"].rank(
ascending=False).astype('int')
data["Party Code"] = data["Party"].apply(get_code)
if df is None:
df = data
else:
df = df.append(data)
fname = os.path.join(base_dir, "all_candidate.csv")
df.to_csv(fname, index=False)
if __name__ == "__main__":
main()