-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDataQuality_v2.py
More file actions
375 lines (326 loc) · 13 KB
/
DataQuality_v2.py
File metadata and controls
375 lines (326 loc) · 13 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
print(r''' _____________________________________________________________
| ____ _ ___ _ _ _ |
| | _ \ __ _| |_ __ _ / _ \ _ _ __ _| (_) |_ _ _ |
| | | | |/ _` | __/ _` | | | | | | | |/ _` | | | __| | | | |
| | |_| | (_| | || (_| | | |_| | |_| | (_| | | | |_| |_| | |
| |____/ \__,_|\__\__,_|___\__\_\\__,_|\__,_|_|_|\__|\__, | |
| |_____| Version: 2.2 |___/ |
| |
| Author: BenMoose39 Initial Release: 12-Apr-2021 |
| https://github.com/benmoose39/Data-Quality |
|_____________________________________________________________|
''')
###########################################################
def end():
try:
input(f"Press ENTER to exit...")
except KeyboardInterrupt:
sys.exit()
sys.exit()
###########################################################
def import_dependencies():
print(f"[*] Checking dependencies... ", end='')
while(True):
try:
global pd
import pandas as pd
import csv
from openpyxl import Workbook
print(f"[OK]")
break
except ModuleNotFoundError as nomodule:
print(f"\n[!] {nomodule}")
module = str(nomodule)[17:-1]
if input(f"[?] Attempt to install {module}?(y/N) ") in yes:
if os.system(f"pip install {module}") == 0:
continue
print(f"[!] Unable to install {module}. Try manually and come back")
end()
return
############################################################
def csv_to_excel(filename, df):
print(f"[*] Converting to excel... ", end='')
writer = pd.ExcelWriter(f"converted_{filename}.xlsx")
df.to_excel(writer, index=False)
writer.save()
print(f"[OK]")
return
############################################################
def excel_to_csv(filename):
try:
print(f'[*] Converting file to {filename}.csv... ', end='')
file = pd.DataFrame(pd.read_excel(f"{filename}.xlsx"))
file.to_csv(f"{filename}.csv", index=False)
print(f"[OK]")
except FileNotFoundError:
print(f"[!] File not found in current directory")
end()
return
############################################################
def file_import():
file = input(f"[?] Name of file to read: ")
excel = False
csv = False
if file not in os.listdir():
print(f"[!] File not found in current directory")
end()
if file.endswith('.xlsx'):
excel = True
elif file.endswith('.csv'):
csv = True
else:
print(f"[!] Unsupported file format.")
end()
filename = '.'.join(file.split('.')[:-1])
if excel:
excel_to_csv(filename)
df = dataframe(filename)
return filename, excel, csv, df
############################################################
def dataframe(filename):
delimiter = input('[?] Delimiter? ')
encoding_list = ['utf-8', 'cp1252']
for enc in encoding_list:
try:
print(f"[*] Trying encoding={enc}...", end='\t')
df = pd.read_csv(f"{filename}.csv", sep=delimiter, encoding=enc, low_memory=False)
print('[OK]')
return df
except UnicodeDecodeError:
print('[Failed]')
except ValueError:
print(f"\n[!] Error: Enter the correct delimiter")
end()
############################################################
def choose():
print("[*] Choose:")
print("\t1) Convert to csv")
print("\t2) Convert to excel(beta)")
print("\t3) Check for duplicate records")
print("\t4) Profile the dataset")
print("\t5) Primary_Key suggester")
print("\t6) Copy profiling reports to `Profiles` folder")
print("\t0) Quit")
while True:
try:
option = int(input('[?] Enter your option: '))
if option == 0:
end()
elif option not in range(0,7):
print('[!] Invalid option')
continue
break
except ValueError:
print("[!] Options are 0,1,2,3,4,5,6")
return option
############################################################
def null_unique_report(file):
rows = len(file)
print(f"\n[INFO]\n[*] Rows: {rows}\n[*] Columns: {len(list(file.columns))}")
print(f"[*] Number of duplicate records: {file.duplicated().sum()}")
null_count = list(file.isnull().sum())
column_list = []
null_list = []
null_percent =[]
for column in list(file.columns):
null_in_col = file[column].isnull().sum()
column_list.append(column)
null_list.append(null_in_col)
null_percent.append(round(null_in_col*100/rows, 3))
null_df = pd.DataFrame({'Attribute':column_list, 'NULL_count':null_list, 'Percentage':null_percent}, index=['' for i in range(len(list(file.columns)))])
distinct_list = []
distinct_percent = []
for column in list(file.columns):
distinct = len(list(file[column].unique()))
if file[column].isnull().any():
distinct -= 1
distinct_list.append(distinct)
distinct_percent.append(round(distinct*100/rows, 3))
distinct_df = pd.DataFrame({'Attribute':column_list, 'Distinct_count':distinct_list, 'Percentage':distinct_percent}, index=['' for i in range(len(list(file.columns)))])
df = pd.DataFrame({'Attribute':column_list, 'NULL_count':null_list, 'NULL_Percentage':null_percent, 'Unique_count':distinct_list, 'Unique_Percentage':distinct_percent})
print(f'[*] Writing to report_{filename}.csv ...', end='')
df.to_csv(f'report_{filename}.csv', index=False)
print(f'[OK]')
write = input('[?] Write to txt? (y/N)')
if write == 'y' or write == 'Y':
print(f'[*] Writing to report_{filename}.txt ...', end='')
with open(f"report_{filename}.txt", 'w') as f:
f.write(f"[INFO]\n[*]Rows: {rows}\n[*]Columns: {len(list(file.columns))}\n")
f.write(f"[*]Number of duplicate records: {file.duplicated().sum()}\n\n")
f.write('********************NULL*********************\n')
f.write(null_df.to_string())
f.write('\n\n\n')
f.write('******************DISTINCT*******************\n')
f.write(distinct_df.to_string())
print(f'[OK]')
return df
############################################################
def distinct_report(file):
rem = len(list(file))
file = file.replace(math.nan, 'NULL', regex = True)
attributes = []
uniq_val = []
val_count = []
flag = False
print(f"\n[*] Finding distinct values---")
print(f"[*] Total number of attributes: {rem}")
for column in list(file):
flag = True
values = list(file[column])
values = ['NULL' if (value is None or value == '') else value for value in values]
unique_list = list(file[column].unique())
for item in unique_list:
if item == '' or item is None:
item = 'NULL'
if flag:
attributes.append(column)
flag = False
else:
attributes.append('')
uniq_val.append(item)
val_count.append(values.count(item))
rem -= 1
print(f"[*] {rem} attributes remaining. Current attribute: {column}")
attributes.append('')
uniq_val.append('')
val_count.append('')
print(f"[*] Writing data to distinct_values_{filename}.csv... ", end='')
dict = {'Column' : attributes, 'Distinct Value' : uniq_val, 'Count' : val_count}
df = pd.DataFrame(dict)
df.to_csv(f"distinct_values_{filename}.csv", index=False)
print(f"[OK]")
return df
############################################################
def profile(null_sheet, distinct_sheet):
print(f"[*] Writing to Profile_{filename}.xlsx...")
wb = Workbook()
writer = pd.ExcelWriter(f"Profile_{filename}.xlsx")
try:
null_sheet.to_excel(writer, 'NULL and UNIQUE', index=False)
distinct_sheet.to_excel(writer, 'DISTINCT VALUES', index=False)
except ValueError as v_error:
print(f"[!] Error: {v_error}")
print(f"[!] Suggestion: Split the distinct_value file and save as two separate files")
end()
writer.save()
print('-' * 50)
print(f"[SUCCESS]Profile_{filename}.xlsx created")
print('-' * 50)
return
############################################################
def check_duplicates(df):
duplicate = df[df.duplicated()]
dups = df.duplicated().sum()
print(f"[*] {dups} duplicate records found.")
if dups > 0:
if input(f"[?] Create new csv with unique records?(y/N) ") in yes:
clean_df = df.drop_duplicates()
print(f"[*] Writing clean records to csv... ", end='')
clean_df.to_csv(f"{filename}_NO_DUPLICATES.csv", index=False)
print(f"[OK]")
############################################################
def pk_suggester(df):
attribs = {}
key = 1
for column in df.columns:
attribs[key] = column
key += 1
print(f"[*] Listing columns...")
for key in attribs:
print(f"\t{key} : {attribs[key]}")
while True:
try:
check = list(map(int, input('[?] Enter keys corresponding to the columns you want in the pkey, separated by commas: ').split(',')))
for key in check:
if key not in attribs:
raise ValueError
except ValueError:
print(f"[!] Invalid key found in the input. Retrying...")
continue
break
pk = [attribs[key] for key in check]
dups = df.duplicated(pk).sum()
if dups == 0:
print(f"[*] Good pkey choice!!")
else:
print(f"[!] {dups} duplicates based on your your pkey combination.")
return
############################################################
def copy_to_profiles():
folder_name = 'Profiles'
folders = [folder for folder in os.listdir() if folder.startswith('Output_')]
profiles = []
print(f"[*] {len(folders)} output folders found.")
copy_count = 0
for folder in folders:
for profile in os.listdir(folder):
if profile.startswith('Profile_') and os.path.isfile(f"{folder}/{profile}"):
profiles.append(os.path.realpath(f"{folder}/{profile}"))
if len(profiles) == 0:
print(f'[*] No files to copy')
end()
else:
print(f"[*] {len(profiles)} profiling reports found.")
print(f"[*] Copying...")
if folder_name not in os.listdir():
print(f"[*] Creating folder... ", end='')
os.mkdir(folder_name)
print(f"[OK]")
#print(f"[*] Copying {len(profiles) profiles...}")
for profile in profiles:
try:
shutil.copy(profile, folder_name)
copy_count += 1
except:
print('[!] Some error occured; Please copy manually.')
end()
if copy_count != len(profiles):
print(f'[!] {len(profiles)-copy_count} files could not be copied')
print(f'[*] {copy_count} files copied successfully')
return
############################################################
try:
yes = ['y','Y']
import sys
import os
import math
import shutil
import_dependencies()
from openpyxl import Workbook
operation = choose()
if operation == 1: #convert to csv
filename, excel, csv, df = file_import()
if csv:
print(f"[*] File is already in csv format")
else:
excel_to_csv(filename)
elif operation == 2: #convert to xlsx
filename, excel, csv, df = file_import()
if excel:
print(f"[*] File is already in xlsx format")
else:
csv_to_excel(filename, df)
elif operation == 3: #check duplicate records
filename, excel, csv, df = file_import()
check_duplicates(df)
elif operation == 4: #Profile
filename, excel, csv, df = file_import()
try:
os.chdir(f"Output_{filename}")
except FileNotFoundError:
print(f"[*] Creating folder: Output_{filename}... ", end='')
os.mkdir(f"Output_{filename}")
print(f'[OK]')
os.chdir(f"Output_{filename}")
null_unique = null_unique_report(df)
distinct = distinct_report(df)
profile(null_unique, distinct)
elif operation == 5:
filename, excel, csv, df = file_import()
pk_suggester(df)
elif operation == 6:
copy_to_profiles()
end()
except KeyboardInterrupt:
print(f"[!] Stopped")
end()