-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata processing tips
More file actions
109 lines (81 loc) · 3.69 KB
/
Copy pathdata processing tips
File metadata and controls
109 lines (81 loc) · 3.69 KB
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
https://towardsdatascience.com/why-and-how-to-use-pandas-with-large-data-9594dda2ea4c
1. Read CSV file data in chunk size
# read the large csv file with specified chunksize
df_chunk = pd.read_csv(r'../input/data.csv', chunksize=1000000)
chunk_list = [] # append each chunk df here
# Each chunk is in df format
for chunk in df_chunk:
# perform data filtering
chunk_filter = chunk_preprocessing(chunk)
# Once the data filtering is done, append the chunk to list
chunk_list.append(chunk_filter)
# concat the list into dataframe
df_concat = pd.concat(chunk_list)
2. Filter out unimportant columns to save memory
# Filter out unimportant columns
df = df[['col_1','col_2', 'col_3', 'col_4', 'col_5', 'col_6','col_7', 'col_8', 'col_9', 'col_10']]
3. Change dtypes for columns
# Change the dtypes (int64 -> int32)
df[['col_1','col_2',
'col_3', 'col_4', 'col_5']] = df[['col_1','col_2',
'col_3', 'col_4', 'col_5']].astype('int32')
# Change the dtypes (float64 -> float32)
df[['col_6', 'col_7',
'col_8', 'col_9', 'col_10']] = df[['col_6', 'col_7',
'col_8', 'col_9', 'col_10']].astype('float32')
https://towardsdatascience.com/the-simple-yet-practical-data-cleaning-codes-ad27c4ce0a38
1. Drop multiple columns
def drop_multiple_col(col_names_list, df):
'''
AIM -> Drop multiple columns based on their column names
INPUT -> List of column names, df
OUTPUT -> updated df with dropped columns
------
'''
df.drop(col_names_list, axis=1, inplace=True)
return df
2.Change dtypes
def change_dtypes(col_int, col_float, df):
'''
AIM -> Changing dtypes to save memory
INPUT -> List of column names (int, float), df
OUTPUT -> updated df with smaller memory
------
'''
df[col_int] = df[col_int].astype('int32')
df[col_float] = df[col_float].astype('float32')
3. Convert categorical variable to numerical variable
def convert_cat2num(df):
# Convert categorical variable to numerical variable
num_encode = {'col_1' : {'YES':1, 'NO':0},
'col_2' : {'WON':1, 'LOSE':0, 'DRAW':0}}
df.replace(num_encode, inplace=True)
4. Check missing data -- Which columns have higher number of missing data
def check_missing_data(df):
# check for any missing data in the df (display in descending order)
return df.isnull().sum().sort_values(ascending=False)
5. Remove strings in columns
def remove_col_str(df):
# remove a portion of string in a dataframe column - col_1
df['col_1'].replace('\n', '', regex=True, inplace=True)
# remove all the characters after &# (including &#) for column - col_1
df['col_1'].replace(' &#.*', '', regex=True, inplace=True)
6. Remove white space in columns
def remove_col_white_space(df,col):
# remove white space at the beginning of string
df[col] = df[col].str.lstrip()
7. Concatenate two columns with strings (with condition)
def concat_col_str_condition(df):
# concat 2 columns with strings if the last 3 letters of the first column are 'pil'
mask = df['col_1'].str.endswith('pil', na=False)
col_new = df[mask]['col_1'] + df[mask]['col_2']
col_new.replace('pil', ' ', regex=True, inplace=True) # replace the 'pil' with emtpy space
8. Convert timestamp(from string to datetime format)
def convert_str_datetime(df):
'''
AIM -> Convert datetime(String) to datetime(format we want)
INPUT -> df
OUTPUT -> updated df with new datetime format
------
'''
df.insert(loc=2, column='timestamp', value=pd.to_datetime(df.transdate, format='%Y-%m-%d %H:%M:%S.%f'))