-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_manipulation.py
153 lines (91 loc) · 3.05 KB
/
data_manipulation.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
"""
Module to manipulate data
Author: Son Gyo Jung
Email: [email protected]
"""
import os
import pandas as pd
import numpy as np
import joblib
from sklearn.preprocessing import MinMaxScaler
class data_manipulation():
"""
Class created for data manipulation process
args:
(1) path_to_file (type:str) - path to the file of interest
return:
(1) pandas.Dataframe of manipulated data set
"""
def __init__(self, path_to_file):
self.df = joblib.load(path_to_file)
def drop_col(self, cols=[]):
"""
Drops unwanted columns
args:
(1) columns (list) - list of columns to drop
returns:
(1) pandas.Dataframe
"""
self.df = self.df.drop(cols_to_drop, axis = 1)
return self.df
def none2zero(self, cols=[]):
"""
Convert None into zero
args:
(1) columns (list) - list of columns
returns:
(1) pandas.Dataframe
"""
for c in cols:
self.df[c] = self.df[c].map(lambda x: 0 if x == 'None' else x)
return self.df
def assign2integer(self, cols):
"""
Assign type to integer
args:
(1) columns (list) - list of columns
returns:
(1) pandas.Dataframe
"""
for c in cols:
self.df[c] = self.df[c].astype(int)
return self.df
def single_entry_col(self):
"""
Find single entry columns
args: None
returns:
(1) list of columns with single entry
"""
singleentry_columns = list()
for c in self.df.columns:
self.df[c] = self.df[c].astype('float')
if self.df[self.df[c].isnull()].shape[0] / float(len(self.df)) >= 1:
empty_columns.append(c)
elif self.df[c].nunique() <= 1:
singleentry_columns.append(c)
return singleentry_columns
def empty_col(self):
"""
Find empty columns
args: None
returns:
(1) list of empty columns
"""
empty_columns = list()
for c in self.df.columns:
self.df[c] = self.df[c].astype('float')
if self.df[self.df[c].isnull()].shape[0] / float(len(self.df)) >= 1:
empty_columns.append(c)
return empty_columns
def OHE(self, categorical_cols):
"""
One-hot-encoding of categorical columns
args:
(1) categorical_cols (list) - list of categorical columns
return:
(1) one-hot-encoded categorical features
"""
df_ohe = pd.get_dummies(data = self.df, columns = categorical_cols, prefix_sep = '_ohe_', drop_first = False)
ohe_cols = [i for i in df_ohe.columns if '_ohe_' in i]
return ohe_cols