-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_tables.py
248 lines (185 loc) · 8.8 KB
/
clean_tables.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
"""
This module is used to clean tables and datasets, such nulls and duplicates, to prepare it for the data model.
The tables are:
- Immigration dataset
- Temperature dataset
- U.S demographic dataset
- Airport dataset
"""
from pyspark.sql.functions import isnan, when, count, col, upper
import pandas as pd
import numpy as np
import re
def drop_nulls_duplicates(df):
"""
Drop columns with more than 90% null values, drop duplicate rows, and drop rows where its values all are missing using pandas.
Inputs:
df (pd.DataFrame): a pandas dataframe
Outputs:
pd.DataFrame: cleaned datafeame
"""
# percentage of null values
perc_nulls = round((df.isna().sum() / df.shape[0]) * 100, 2)
print("Percentage of null values:")
print(perc_nulls.to_string())
print()
# drop columns with more than 90% null values
drop_cols = list(perc_nulls[perc_nulls > 90].index)
if len(drop_cols) == 0:
print("No columns to be dropped")
else:
print("Columns to be dropped:", drop_cols)
clean_df = df.drop(columns=drop_cols)
print()
# drop duplicate rows
num_duplicate = clean_df.duplicated().sum()
print("Number of duplicate rows:", num_duplicate)
print()
clean_df = clean_df.drop_duplicates()
# drop rows where its values are missing
num_rows_all_miss = clean_df[clean_df.isnull().all(axis=1)].shape[0]
print("Number of rows where all values are missing:", num_rows_all_miss)
if num_rows_all_miss > 0:
print("Drop rows where all values are missing")
print()
clean_df = clean_df.dropna(how='all')
# drop miss index columns (exploration appears: ['Unnamed: 0'])
miss_index_cols = [col for col in list(clean_df.columns) if 'unnamed' in col.lower()]
print("Drop miss index columns if exist (Unnamed):", miss_index_cols)
clean_df = clean_df.drop(miss_index_cols, axis=1)
return clean_df
def drop_nulls_duplicates_spark(df):
"""
drop columns with more than 90% null values, drop duplicate rows, and drop rows where its values all are missing using spark.
Inputs:
df (pyspark DataFrame): a pyspark dataframe
Outputs:
pyspark DataFrame: cleaned datafeame
"""
# percentage of null values
perc_nulls = ((df.select([count(when(col(c).contains('None') | \
col(c).contains('NULL') | \
(col(c) == '') | \
col(c).isNull() | \
isnan(c), c
)).alias(c)
for c in df.columns]).toPandas() / df.count()) * 100)
perc_nulls = pd.melt(perc_nulls, var_name='col_name', value_name='perc')
perc_nulls = perc_nulls.set_index(perc_nulls.columns[0]).squeeze()
print("Percentage of null values:")
print(perc_nulls.to_string())
print()
# drop columns with more than 90% null values (exploration appears: ['occup', 'entdepu', 'insnum'])
drop_cols = list(perc_nulls[perc_nulls > 90].index)
if len(drop_cols) == 0:
print("No columns to be dropped")
else:
print("Columns to be dropped:", drop_cols)
clean_df = df.drop(*drop_cols)
print()
# drop duplicate rows
num_records_before_dup = clean_df.count()
clean_df = clean_df.dropDuplicates()
num_records_after_dup = clean_df.count()
num_duplicate = num_records_before_dup - num_records_after_dup
print("Number of duplicate rows:", num_duplicate)
print()
# drop rows where its values are missing
clean_df = clean_df.dropna(how='all')
num_records_after_row_miss = clean_df.count()
num_rows_all_miss = num_records_after_dup - num_records_after_row_miss
print("Number of rows where all values are missing:", num_rows_all_miss)
if num_rows_all_miss > 0:
print("Drop rows where all values are missing")
# drop miss index columns
miss_index_cols = [col for col in list(clean_df.columns) if 'unnamed' in col.lower()]
print("\nDrop miss index columns if exist (Unnamed):", miss_index_cols)
clean_df = clean_df.drop(*drop_cols)
return clean_df
def clean_immigration_table(input_df, process_spark=False):
"""
Clean the immigration table using spark or pandas.
Inputs:
input_df (dataframe): pandas or pyspark dataframe (process_spark argument changes accordingly)
process_spark (bool, optional): whether to process cleaning the input dataframe using spark or pandas. Defaults to False.
Outputs:
pyspark/pandas dataframe: cleaned immigration dataframe
"""
# process the cleaning using spark or pandas based on the flag parameter and input dataframe type
# data exploration appears that these columns will be dropped due null values problems: ['occup', 'entdepu', 'insnum'])
# make sure that i94addr state name are all capitalized
if process_spark:
clean_immigration_df = drop_nulls_duplicates_spark(input_df)
clean_immigration_df = clean_immigration_df.select("*", upper("i94addr"))
else:
# data exploration appears that these columns will be dropped due to miss indexing problems: ['Unnamed: 0'])
clean_immigration_df = drop_nulls_duplicates(input_df)
clean_immigration_df['i94addr'] = clean_immigration_df['i94addr'].str.upper()
return clean_immigration_df
def clean_temperature_table(input_df):
"""
Clean the temperature table.
Inputs:
input_df (pd.DataFrame): input dataframe
Outputs:
pd.DataFrame: cleaned temperature dataframe
"""
# create copy of input dataframe for cleaning
clean_temperature_df = input_df.copy()
# convert dt column type from object type to datetime type
clean_temperature_df['dt'] = pd.to_datetime(clean_temperature_df['dt'])
# get only temperature from 2010 and above
clean_temperature_df = clean_temperature_df[clean_temperature_df['dt'] >= '2010-01-01']
# titleize the country to unify the the country naming
clean_temperature_df['Country'] = clean_temperature_df['Country'].str.title()
# get only rows with non-null values of temperature
clean_temperature_df = clean_temperature_df.dropna(subset=['AverageTemperature', 'AverageTemperatureUncertainty'], how='all')
# check for nulls and duplicates
clean_temperature_df = drop_nulls_duplicates(clean_temperature_df)
# convert Latitude & Longitude columns to numerical values
def convert_to_numeric_coord(coord):
num = re.findall("\d+\.\d+", coord)
if coord[-1] in ["W", "S"]:
return -1. * float(num[0])
else:
return float(num[0])
clean_temperature_df['Latitude'] = clean_temperature_df['Latitude'].apply(convert_to_numeric_coord)
clean_temperature_df['Longitude'] = clean_temperature_df['Longitude'].apply(convert_to_numeric_coord)
return clean_temperature_df
def clean_demographics_table(input_df):
"""
Clean the demographics table.
Inputs:
input_df (pd.DataFrame): input dataframe
Outputs:
pd.DataFrame: cleaned demographics dataframe
"""
# check for nulls and duplicated for demographics_df
clean_demographics_df = drop_nulls_duplicates(input_df)
# make sure that state code are all capitalized
clean_demographics_df['State Code'] = clean_demographics_df['State Code'].str.upper()
return clean_demographics_df
def clean_airport_table(input_df):
"""
Clean the airport table.
Inputs:
input_df (pd.DataFrame): input dataframe
Outputs:
pd.DataFrame: cleaned airport dataframe
"""
# create copy of input dataframe for cleaning
clean_airport_df = input_df.copy()
# make the airport name titleized
clean_airport_df['name'] = clean_airport_df['name'].str.title()
# convert the value '0, 0' in coordinates column to nan, since '0, 0' is origin coordinates - above the ocean (data exploration)
clean_airport_df.loc[clean_airport_df['coordinates'] == '0, 0', 'coordinates'] = np.nan
# convert the value '0' in iata_code column to nan, since there is no '0' as iata_code (data exploration)
clean_airport_df.loc[clean_airport_df['iata_code'] == '0', 'iata_code'] = np.nan
# check for nulls and duplicated for airport_df
clean_airport_df = drop_nulls_duplicates(clean_airport_df)
# convert coordinates column to longitude & latitude columns with float data type
clean_airport_df[['longitude', 'latitude']] = clean_airport_df['coordinates'].apply(lambda x: pd.Series(list(map(float, map(str.strip, x.split(","))))) \
if x is not np.nan else pd.Series([np.nan, np.nan]))
# drop coordinates column
clean_airport_df = clean_airport_df.drop(['coordinates'], axis=1)
return clean_airport_df