-
Notifications
You must be signed in to change notification settings - Fork 3
/
preprocessing.py
361 lines (341 loc) · 15.1 KB
/
preprocessing.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 21 05:54:26 2018
@author: Awais: awaisrauf.github.io
@descrption: Function related to cleaning and preprocessing of raw data
"""
import pandas as pd
#==============================================================================
# Proprcoesses voter distribution data
#==============================================================================
def Vote_distribution_preprocessed():
df_NA_voter_dist = pd.read_csv("E:\Semester3\GE2018\election\data\Election_2018_Stats\\Voter_Distribution_Lat_Lng.csv")
# Change constituency number from format NA-240 Rawalpindi to NA-240
constituencies = df_NA_voter_dist["Constituency"].str.split(" ")
constituencies = pd.DataFrame(constituencies.values.tolist()).add_prefix('constituency_')
constituencies = constituencies.drop(constituencies.columns[[1,2]], axis=1)
df_NA_voter_dist["Constituency"] = constituencies
return df_NA_voter_dist
def NA_list_preprocessed():
df_NA_list = pd.read_csv("E:\Semester3\GE2018\election\data\Election_2018_Stats\\NA_List.csv")
# Replace full party names with their symbols
# Mannual Replacements are preffered as there are many similar names of parties
replacements = {
# standredize Independent name in data
'Independent': 'IND',
'Independent Candidate': 'IND',
' Independent': 'IND',
'Indepdendent': 'IND',
# standredize PTI name in data
'Pakistan Tehreek-e-lnsaf':'PTI',
'Pakistan Tehreek e Insaf': 'PTI',
'Pakistan Tehreek E Insaf': 'PTI',
'Pakistan Tehreek e Insaaf': 'PTI',
'Pakistan tehreek-e-Insaf': 'PTI',
'Pakistan Tehreek e insaf': 'PTI',
'PakistanTehreek-e-Insaf': 'PTI',
'Pakisyan Tehreek e Insaaf': 'PTI',
'Pakistan Tehreek-e-lnsaf': 'PTI',
'Pakistan tehreek e Insaf': 'PTI',
'Pakistan Tehreek-e-Insaf': 'PTI',
'Pakistan Tahreek-e-Insafa': 'PTI',
'Tehrik e Insaf': 'PTI',
# standredize PML-N name in data
'Pakistan Muslim League (N)': 'PML-N',
'Pakistan Muslim League(N)': 'PML-N',
'Pakistan Muslim League\n(N)': 'PML-N',
'PMLN': 'PML-N',
'PMLN': 'PML-N',
'PML (N)': 'PML-N',
'PML -N': 'PML-N',
'Pakistan Muslim League(N)' : 'PML-N',
# standredize PPPP name in data
'Pakistan Peoples Party Parliamentarians': 'PPPP',
'Pakistan Peoples party parlimentarians':'PPPP',
'Pakistan Peoples party parlimentarians': 'PPPP',
'Pakistan Peoples Party Parlimentarians': 'PPPP',
'Pakistan Peoples Party\nParliamentarians': 'PPPP',
'Pakistan Peoples Party': 'PPPP',
'Pakistan Peoples Part Parliamentarians': 'PPPP',
'Pakistan peopkes party parlimentarians': 'PPPP',
'Pakistan People Party Parlianmentray': 'PPPP',
#
'MUTAHIDA MAJLIS AMAL PAKISTAN': 'MMA',
'Mutahida Majlas-e-Amal Pakistan':'MMA',
'MUTAHIDA MAJLIS AMAL PAKISTAN ': 'MMA',
'Mutthida Majlis-E-Amal Pakistan': 'MMA',
'Mutthida Majlis-e-Amal Pakistan': 'MMA',
'Mutahida Majlas-e-Amal Pakistan': 'MMA',
'MUTTHIDA MAJLIS-E-AMAL PAKISTAN': 'MMA',
'MUTTAHIDA MAJLIS-E- AMAL PAKISTAN': 'MMA',
'Muthida Majlis e Amal Pakistan': 'MMA',
'Mutihida Majlis e Amal Pakistan': 'MMA',
'M.M.A' : 'MMA',
#
'Mutahida Qaumi Movemebt Pakistan': 'MQM',
'Muttahida Qaumi Movement Pakistan': 'MQM',
#
'Tehreek Labbaik Pakistan': 'TLP',
'Tehreek labbaik Pakistan': 'TLP',
'Tehrik e Labbaik': 'TLP',
'Tahreek Labbaik pakistan': 'TLP',
'Tehreek e Labbaik Pakistan': 'TLP',
'Tehrek e labbaik Pakistan' : 'TLP',
'Tehrik e Labbaik Pakistan': 'TLP',
'Tehrik e Labbaik': 'TLP',
'Tehrik e Labbaik': 'TLP',
'Tehreek e labbaik Pakistan':'TLP',
'Tehreek-e-Labbaik Pakistan': 'TLP',
'Tehreek e Labbaik Pakistan TLI ': 'TLP',
#
'Awami National Party': 'ANP',
'Avvami National Party': 'ANP',
#
'Pak Sarzameen Party': 'PSP',
'Pak SarZamen Party': 'PSP',
#
'Pakistan Muslim League': 'PML-Q',
'PML -Q':'PML-Q',
#
'Balochistan National Party': 'BNP',
'Balochistan National Party': 'BNP',
#
'Balochistan Awami Party': 'BAP',
'Balochistan Awami Party': 'BAP',
#
'Pashtoonkhwa Milli Awami Party': 'PKMAP',
'PashtoonKhwa Milli Awami party': 'PKMAP',
'Pakhtoonkhwa Milli Awami Party ': 'PKMAP',
'Pashtoonkhawa Milli Awami Party': 'PKMAP',
'Pashtunkhwa Milli Awami Party ': 'PKMAP',
#
'All Pakistan Muslim League': 'APML',
#
'Grand Democratic Allience': 'GDA',
'G.D.A': 'GDA',
#
'Sindh United Party': 'SUP',
#
'Qaumi Watan Party': 'QWP',
"Pakistan Muslim League (Z)":"PML-Z"
}
df_NA_list['Party Affiliation'].replace(replacements, inplace=True)
# add a district as a new column by preprocessing constituency name (Rawalpinid-1 to Rawalpinid)
df_constituency = df_NA_list["Constituency Name"].str.strip() # remove leading spaces
cities = (df_constituency.str.split(" ")) # strips names based on spaces
cities_1 = pd.DataFrame(cities.values.tolist()).add_prefix('constituency_') # retain column that only have district name
cities_1 = cities_1[cities_1.columns[0]]
cities_1 = cities_1.str.split("-")
cities_1 = pd.DataFrame(cities_1.values.tolist()).add_prefix('constituency_')
cities_1 = cities_1[cities_1.columns[0]]
df_NA_list["District"] = cities_1.str.lower()
# solving mismatching districts problem
replacement_districts = {
"gwadar": "gawadar",
"jacababd":"jacobabad",
"jackoabad":"jacobabad",
"batagram":"battagram",
"d.g.":"dgkhan",
"d.i.khan":"dikhan",
"gujrnawala":"guranwala",
"shaheed":"nawabshah",
"sba":"jaffarabad",
"bolan":"kachhi",
"lower.dir":"lower",
"upper.dir":"upper",
"r.y.khan":"ryk",
"rahim":"ryk",
"sahib":"nankana",
"shikarpu":"shikarpur",
"t.t.singh":"toba",
"mastun":"mastung",
"sujawal":"thatta",
"sakkar":"sukkar"
}
df_NA_list['District'].replace(replacement_districts, inplace=True)
# Remove space in constituencies names i.e. if cons_name = "NA-1 ", make it "NA-1"
number_of_constituencies = len(df_NA_list["Constituency Number (ID)"].tolist())
list_corrected_names = []
for i in range(number_of_constituencies):
corrected_name = df_NA_list["Constituency Number (ID)"].iloc[i].strip()
list_corrected_names.append(corrected_name)
# replace all the values
df_NA_list.loc[0:number_of_constituencies,"Constituency Number (ID)"] = list_corrected_names
return df_NA_list
#==============================================================================
# Adds district name from constituency name
# Converts percentage from 55% values to 55 like ints
# Fills nan values in party column with IND = Independent
#==============================================================================
def Perevious_results_preprocessed():
df_election_result_97 = pd.read_csv("E:\Semester3\GE2018\GE2018\election_prediction\data\Perevious_Results\election_results_1997.csv")
df_election_result_02_13 = pd.read_csv("E:\Semester3\GE2018\GE2018\election_prediction\data\Perevious_Results\election_results_2002-2013.csv")
frames = [df_election_result_97,df_election_result_02_13]
df_perevious_results = pd.concat(frames)
#df_election_result_97_13 = df_perevious_results
#df_perevious_results.to_csv("data\Perevious_Results\election_result123")
#df_perevious_results = pd.read_csv("data\Perevious_Results\election_results_1997.csv")
#df_perevious_results = pd.read_csv("data\Perevious_Results\election_result123.csv")
#df_perevious_results =df_election_result_02_13
df_perevious_results["Party"] = df_perevious_results["Party"].fillna(value="IND")
df_perevious_results["Votes"] = df_perevious_results["Votes"].fillna(value=0)
df_perevious_results = df_perevious_results.dropna()
# add a district as a new column by preprocessing constituency name
df_constituency = df_perevious_results["constituency"].str.strip()
cities = (df_constituency.str.split(" "))
#cities = cities.astype(str)
abc = pd.DataFrame(cities.values.tolist()).add_prefix('constituency_')
constituency = abc[abc.columns[0]]
abc = abc[abc.columns[1]]
abc = abc.str.split("-")
abc = pd.DataFrame(abc.values.tolist()).add_prefix('constituency_')
abc = abc[abc.columns[0]]
df_perevious_results["District"] = abc.str.lower()
df_perevious_results["Constituency"] = constituency
# solving mismatching districts problem
replacement_districts = {
"gwadar": "gawadar",
"jacababd":"jacobabad",
"jackoabad":"jacobabad",
"batagram":"battagram",
"d.g.":"dgkhan",
"d.i.khan":"dikhan",
"gujrnawala":"guranwala",
"sba":"jaffarabad",
"bolan":"kachhi",
"lower.dir":"lower",
"upper.dir":"upper",
"r.y.khan":"ryk",
"rahim":"ryk",
"sahib":"nankana",
"shikarpu":"shikarpur",
"t.t.singh":"toba",
"mastun":"mastung",
"sujawal":"thatta",
"sakkar":"sukkar",
"k":"karachi",
"korang":"karachi",
"malir":"karachi"
}
df_perevious_results['District'].replace(replacement_districts, inplace=True)
# change turnout from percentage(e.g. 55%) to int (e.g. 55)
turnout = df_perevious_results["Turnout"].str.split("%")
turnout = pd.DataFrame(turnout.values.tolist()).add_prefix('turnout_')
turnout = turnout.drop(turnout.columns[[1]], axis=1).astype(float)
df_perevious_results["Turnout"] = turnout
# fills nan values in party affiliation with IND
df_perevious_results["Party"].fillna("IND", inplace = True)
# replace PML with PML-Q as its name was changes
df_perevious_results["Party"].replace("PML","PML-Q", inplace = True)
return df_perevious_results
#==============================================================================
# Preprocessed result 2018
#
#==============================================================================
def Result_2018():
df_result_2018 = pd.read_csv("E:\Semester3\GE2018\GE2018\election_prediction\data\Election_2018_Stats\Election_result_2018.csv")
df_NA_18 = pd.read_csv("E:\\Semester3\\GE2018\\GE2018\\election_prediction\\data\\\Election_2018_Stats\\NA_list.csv")
# fill na values with values above
df_result_2018 = df_result_2018.fillna(method="ffill")
replacements = {
# standredize Independent name in data
'Independent': 'IND',
'Independent Candidate': 'IND',
' Independent': 'IND',
'Indepdendent': 'IND',
# standredize PTI name in data
'Pakistan Tehreek-e-lnsaf':'PTI',
'Pakistan Tehreek e Insaf': 'PTI',
'Pakistan Tehreek E Insaf': 'PTI',
'Pakistan Tehreek e Insaaf': 'PTI',
'Pakistan tehreek-e-Insaf': 'PTI',
'Pakistan Tehreek e insaf': 'PTI',
'PakistanTehreek-e-Insaf': 'PTI',
'Pakisyan Tehreek e Insaaf': 'PTI',
'Pakistan Tehreek-e-lnsaf': 'PTI',
'Pakistan tehreek e Insaf': 'PTI',
'Pakistan Tehreek-e-Insaf': 'PTI',
'Pakistan Tahreek-e-Insafa': 'PTI',
'Tehrik e Insaf': 'PTI',
# standredize PML-N name in data
'Pakistan Muslim League (N)': 'PML-N',
'Pakistan Muslim League(N)': 'PML-N',
'Pakistan Muslim League\n(N)': 'PML-N',
'PMLN': 'PML-N',
'PMLN': 'PML-N',
'PML (N)': 'PML-N',
'PML -N': 'PML-N',
'Pakistan Muslim League(N)' : 'PML-N',
# standredize PPPP name in data
'Pakistan Peoples Party Parliamentarians': 'PPPP',
'Pakistan Peoples party parlimentarians':'PPPP',
'Pakistan Peoples party parlimentarians': 'PPPP',
'Pakistan Peoples Party Parlimentarians': 'PPPP',
'Pakistan Peoples Party\nParliamentarians': 'PPPP',
'Pakistan Peoples Party': 'PPPP',
'Pakistan Peoples Part Parliamentarians': 'PPPP',
'Pakistan peopkes party parlimentarians': 'PPPP',
'Pakistan People Party Parlianmentray': 'PPPP',
#
'MUTAHIDA MAJLIS AMAL PAKISTAN': 'MMA',
'Mutahida Majlas-e-Amal Pakistan':'MMA',
'MUTAHIDA MAJLIS AMAL PAKISTAN ': 'MMA',
'Mutthida Majlis-E-Amal Pakistan': 'MMA',
'Mutthida Majlis-e-Amal Pakistan': 'MMA',
'Mutahida Majlas-e-Amal Pakistan': 'MMA',
'MUTTHIDA MAJLIS-E-AMAL PAKISTAN': 'MMA',
'MUTTAHIDA MAJLIS-E- AMAL PAKISTAN': 'MMA',
'Muthida Majlis e Amal Pakistan': 'MMA',
'Mutihida Majlis e Amal Pakistan': 'MMA',
'M.M.A' : 'MMA',
'Muttahida Majlis-e-Amal Pakistan':'MMA',
#
'Mutahida Qaumi Movemebt Pakistan': 'MQM',
'Muttahida Qaumi Movement Pakistan': 'MQM',
#
'Tehreek Labbaik Pakistan': 'TLP',
'Tehreek labbaik Pakistan': 'TLP',
'Tehrik e Labbaik': 'TLP',
'Tahreek Labbaik pakistan': 'TLP',
'Tehreek e Labbaik Pakistan': 'TLP',
'Tehrek e labbaik Pakistan' : 'TLP',
'Tehrik e Labbaik Pakistan': 'TLP',
'Tehrik e Labbaik': 'TLP',
'Tehrik e Labbaik': 'TLP',
'Tehreek e labbaik Pakistan':'TLP',
'Tehreek-e-Labbaik Pakistan': 'TLP',
'Tehreek e Labbaik Pakistan TLI ': 'TLP',
#
'Awami National Party': 'ANP',
'Avvami National Party': 'ANP',
#
'Pak Sarzameen Party': 'PSP',
'Pak SarZamen Party': 'PSP',
#
'Pakistan Muslim League': 'PML-Q',
'PML -Q':'PML-Q',
#
'Balochistan National Party': 'BNP',
'Balochistan National Party': 'BNP',
#
'Balochistan Awami Party': 'BAP',
'Balochistan Awami Party': 'BAP',
#
'Pashtoonkhwa Milli Awami Party': 'PKMAP',
'PashtoonKhwa Milli Awami party': 'PKMAP',
'Pakhtoonkhwa Milli Awami Party ': 'PKMAP',
'Pashtoonkhawa Milli Awami Party': 'PKMAP',
'Pashtunkhwa Milli Awami Party ': 'PKMAP',
#
'All Pakistan Muslim League': 'APML',
#
'Grand Democratic Allience': 'GDA',
'G.D.A': 'GDA',
#
'Sindh United Party': 'SUP',
#
'Qaumi Watan Party': 'QWP',
"Pakistan Muslim League (Z)":"PML-Z"
}
df_result_2018['results__party'].replace(replacements, inplace=True)
return df_result_2018