-
Notifications
You must be signed in to change notification settings - Fork 0
/
imputation.py
92 lines (86 loc) · 2.07 KB
/
imputation.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
import pandas as pd
def show_columns(file):
print("\nNull values of each Column\n")
print(file.isnull().sum())
def remove_columns(file):
for col in file:
print(col, end=" ")
print("\n")
while True:
cols = input("Enter all the column(s) you want to delete (Press -1 to go back) ")
if cols == '-1':
return
yn = input("Are you sure? (y / n) ")
if yn.lower() == 'n':
continue
elif yn.lower() == 'y':
break
else:
sys.exit("Invalid character")
for col in cols.strip().split(" "):
if col not in file:
sys.exit("Invalid column")
file.drop(columns=[col], inplace=True)
print("Done......")
def fill_mean(file):
for col in file:
print(col, end=" ")
print("\n")
while True:
col = input("Enter the column name (Press -1 to go back) ")
if col == '-1':
return
yn = input("Are you sure? (y / n) ")
if yn.lower() == 'n':
continue
elif yn.lower() == 'y':
break
else:
sys.exit("Invalid character")
if col not in file:
sys.exit("Invalid column")
file[col].fillna(file[col].mean(), inplace=True)
print("Done......")
def fill_median(file):
for col in file:
print(col, end=" ")
print("\n")
while True:
col = input("Enter the column name (Press -1 to go back) ")
if col == '-1':
return
yn = input("Are you sure? (y / n) ")
if yn.lower() == 'n':
continue
elif yn.lower() == 'y':
break
else:
sys.exit("Invalid character")
if col not in file:
sys.exit("Invalid column")
file[col].fillna(file[col].median(), inplace=True)
print("Done......")
def fill_mode(file):
for col in file:
print(col, end=" ")
print("\n")
while True:
col = input("Enter the column name (Press -1 to go back) ")
if col == '-1':
return
yn = input("Are you sure? (y / n) ")
if yn.lower() == 'n':
continue
elif yn.lower() == 'y':
break
else:
sys.exit("Invalid character")
if col not in file:
sys.exit("Invalid column")
file[col].fillna(file[col].mode()[0], inplace=True)
print("Done......")
def show_dataset(file):
n = int(input("\nHow many rows (>0) to print? (Press -1 to go back) "))
if n == -1:
return
print(file.head(n))