-
Notifications
You must be signed in to change notification settings - Fork 3
/
cleaning.py
26 lines (22 loc) · 1023 Bytes
/
cleaning.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
import pandas as pd
# Function converts the obvious numeric columns into int/float
def prelim_numeric_converter(df):
df.loc[:, df.columns.str.startswith('VotingPerformance')] = df.loc[:, df.columns.str.startswith('VotingPerformance')].replace(["%"], "", regex=True)
df.loc[:, df.columns.str.startswith('Commercial')] = df.loc[:, df.columns.str.startswith('Commercial')].replace(["$"], "", regex=True)
cols = df.columns
for c in cols:
try:
df[c] = pd.to_numeric(df[c])
except:
pass
return df
# converting election results to numeric (resource heavy)
def election_numeric_converter(df):
col_len = df.shape[1]-1
df.loc[:, df.columns.str.startswith('Election')] = df.loc[:, df.columns.str.startswith('Election')].replace(["%"], "", regex=True)
for i in range(df.loc[:, df.columns.str.startswith('Election')].shape[1]):
try:
df.iloc[:, col_len-i] = pd.to_numeric(df.iloc[:, col_len-i])
except:
pass
return df