forked from abdullahranginwala/ml-preprocessor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
impute.py
49 lines (42 loc) · 1.71 KB
/
impute.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
import pandas as pd
class Impute:
@staticmethod
def fillwithmean(data, colname):
try:
data[colname] = data[colname].fillna(data[colname].mean())
except KeyError:
raise KeyError(f"colname \"{colname}\" is not present in given CSV file")
except TypeError:
raise TypeError(f"colname \"{colname}\" has not proper data type. try on another column")
return data
@staticmethod
def fillwithmedian(data, colname):
try:
data[colname] = data[colname].fillna(data[colname].median())
except KeyError:
raise KeyError(f"colname \"{colname}\" is not present in given CSV file")
except TypeError:
raise TypeError(f"colname \"{colname}\" has not proper data type. try on another column")
return data
@staticmethod
def fillwithmode(data, colname):
try:
data[colname] = data[colname].fillna(data[colname].mode()[0])
except KeyError:
raise KeyError(f"colname \"{colname}\" is not present in given CSV file")
except TypeError:
raise TypeError(f"colname \"{colname}\" has not proper data type. try on another column")
return data
@staticmethod
def removecol(data, colname):
try:
data.drop(colname.split(" "), axis=1, inplace=True)
except KeyError:
raise KeyError(f"colname \"{colname}\" is not present in given CSV file")
return data
@staticmethod
def nullValues(data):
nullValues = {}
for col in data.columns.values:
nullValues[col] = sum(pd.isnull(data[col]))
return nullValues