-
Notifications
You must be signed in to change notification settings - Fork 0
/
06302023_cleaning_df_bothdata.py
295 lines (255 loc) · 14.1 KB
/
06302023_cleaning_df_bothdata.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 30 20:35:08 2023
Last script in the first half of 2023
Last revised on November 15, 2024
Most recent execution on November 15, 2024
@author: Tiangeng Lu
What are the requirements before running this program?
- NIV and IV .txt files are available in folders.
What's new in this program, compared with previous programs?
- Revised from `06032023_scraped_to_df.py` that only deals with NIV.
- This program includes cleaning of both NIV and IV.
- This program fixes the excessive space in the initially-removed-then-restored rows.
- This program uses less loops and more list comprehentions.
- This program outputs the following:
a) visa_alltime.csv
b) countries.csv
"""
import os
import pandas as pd
from datetime import datetime
####################### NIV #################################
niv_pdf = 'niv'
if os.path.exists(niv_pdf):
print("First document:",sorted(os.listdir(niv_pdf))[0])
print("Last document:",sorted(os.listdir(niv_pdf))[-1])
else:
print("No such directory.")
# folder
niv_pdf_folder = os.getcwd() + '/' + niv_pdf + "/"
# short name
niv_pdf_short = sorted(os.listdir(niv_pdf))
# full name
niv_pdf_full = [niv_pdf_folder + pdf for pdf in niv_pdf_short]
niv_txt = 'nivtxt'
if os.path.exists(niv_txt):
print("First document:",sorted(os.listdir(niv_txt))[0])
print("Last document:",sorted(os.listdir(niv_txt))[-1])
else:
print("No txt folder available.")
# full txt path for all txt
niv_txt_full = [os.getcwd() + '/' + niv_txt + '/' + txt for txt in sorted(os.listdir(niv_txt))]
# mac has an invisible file .DS_Store
niv_txt_full = [txt for txt in niv_txt_full if 'niv_' in txt]
### all txt files into one df
niv_convert_start = datetime.now().strftime("%Y-%m-%d, %H:%M:%S")
print("The conversion from all .txt documents to one dataframe started at:", niv_convert_start)
#pd.read_csv(niv_txt_full[0], delimiter = "\t", header = None)
niv_DF_raw = [None] * len(niv_txt_full)
for n in range(len(niv_txt_full)):
df_file = pd.read_csv(niv_txt_full[n], delimiter = "\t", header = None)
df_file = df_file.rename(columns = {0:'V'})
df_file['V'] = df_file['V'].str.upper().apply(lambda x: x.strip())
# extract the timestamp substring
df_file['time'] = niv_txt_full[n].split('/')[-1].split('.')[0].split('_')[1]
if df_file['V'].str.contains('GRAND TOTAL', case = False).any() == False:
print(n)
else:
remove = df_file.index >= list(df_file[df_file['V'].str.contains('GRAND TOTAL', case = False)].index)[0]
df_file = df_file.iloc[remove == False]
niv_DF_raw[n] = df_file
niv_DF = pd.concat([df_raw for df_raw in niv_DF_raw]).reset_index(drop = True)
niv_convert_end = datetime.now().strftime("%Y-%m-%d, %H:%M:%S")
print("The conversion ended at:", niv_convert_end)
### CLEANING
niv_DF_archive = niv_DF.copy(deep = True)
niv_DF['V'] = niv_DF['V'].apply(lambda x: x.strip())
niv_DF = niv_DF[niv_DF['V'].str.len() > 1]
# don't forget to escape \
niv_headers = ['NONIMMIGRANT','NATIONALITY VISA','PAGE','\(FY', '\#SBU']
niv_DF_headers = niv_DF[niv_DF['V'].str.contains('|'.join(niv_headers))]
niv_removed_initial = list(niv_DF_headers.index)
niv_DF = niv_DF[~niv_DF.index.isin(niv_removed_initial)]
# split the column ['V'] for prelimiary output
niv_DF_final = pd.DataFrame()
niv_DF_final['nationality'] = [' '.join(row.split(' ')[:-2]).strip() for row in niv_DF['V']]
niv_DF_final['visa'] = [row.split(' ')[-2].strip() for row in niv_DF['V']]
niv_DF_final['issue'] = [row.split(' ')[-1].strip() for row in niv_DF['V']]
# wrap with list() to remove original index info
niv_DF_final['time'] = list(niv_DF['time'])
# verify that all issue rows are numbers
not_end_num = [row for row in niv_DF_final['issue'] if row[-1] not in [str(num) for num in list(range(0,10,1))]]
not_start_num = [row for row in niv_DF_final['issue'] if row[0] not in [str(num) for num in list(range(0,10,1))]]
if len(not_end_num) == 0 & len(not_start_num) == 0:
print("The issue column looks good.")
else:
print("There are problems in the issue column. Some of them are not numbers.")
# get country list
niv_countries = sorted(list(set(niv_DF_final['nationality'])))
## RESTORE ROWS that contain both data and header/footer
niv_restored_rows = []
for row in niv_DF_headers['V'] + ',' + niv_DF_headers['time']:
# locate any matches
if any([x in row for x in niv_countries]):
print(row)
niv_restored_rows.append(row)
# construct a new dataframe with restored rows, same structure that has nationality, visa, issue, and time
niv_restored_df = pd.DataFrame()
niv_restored_df['nationality'] = [' '.join(row.split('NONIMMIGRANT')[0].strip().split(' ')[:-2]) for row in niv_restored_rows]
niv_restored_df['visa'] = [row.split('NONIMMIGRANT')[0].strip().split(' ')[-2] for row in niv_restored_rows]
niv_restored_df['issue'] = [row.split('NONIMMIGRANT')[0].strip().split(' ')[-1] for row in niv_restored_rows]
niv_restored_df['time'] = [row.split(',')[-1].strip() for row in niv_restored_rows]
# concatenate the original and the restored rows
niv_DF_final = pd.concat([niv_DF_final, niv_restored_df],axis = 0).\
drop_duplicates().sort_values(by = ['time','nationality','visa']).\
reset_index(drop = True).rename(columns = {'issue':'count'})
if niv_DF_final['count'].dtype != "int":
niv_DF_final['count'] = niv_DF_final['count'].str.replace(',', '')
niv_DF_final['count'] = niv_DF_final['count'].astype('int')
print(niv_DF_final['count'].dtype)
assert niv_DF_final['count'].dtype == "int"
#### added on 08/02/2023, clean country names to reduce their variations
print('Before text cleaning, there are', str(len(set(niv_DF_final['nationality']))), 'country/region names.')
niv_DF_final['nationality'] = niv_DF_final['nationality'].str.replace('*','')
niv_DF_final['nationality'] = niv_DF_final['nationality'].str.replace(r'\s+',' ', regex = True)
niv_DF_final['nationality'] = niv_DF_final['nationality'].str.replace(' - ','-')
niv_DF_final['nationality'] = niv_DF_final['nationality'].str.replace(' – ','-')
niv_DF_final['nationality'] = niv_DF_final['nationality'].str.replace('- ','-')
niv_DF_final['nationality'] = niv_DF_final['nationality'].str.replace(' -','-')
print('After text cleaning, there are', str(len(set(niv_DF_final['nationality']))), 'country/region names.')
# save NIV locally
niv_DF_final.to_csv('niv_alltime.csv', index = False)
####################### IV ##################################
iv_pdf = 'iv'
if os.path.exists(iv_pdf):
print("First document:",sorted(os.listdir(iv_pdf))[0])
print("Last document:",sorted(os.listdir(iv_pdf))[-1])
else:
print("No such directory.")
# folder
iv_pdf_folder = os.getcwd() + '/' + iv_pdf + "/"
# short name
iv_pdf_short = sorted(os.listdir(iv_pdf))
# full name
iv_pdf_full = [iv_pdf_folder + pdf for pdf in iv_pdf_short]
iv_txt = 'ivtxt'
if os.path.exists(iv_txt):
print("First document:",sorted(os.listdir(iv_txt))[0])
print("Last document:",sorted(os.listdir(iv_txt))[-1])
else:
print("No txt folder available.")
# full txt path for all txt
iv_txt_full = [os.getcwd() + '/' + iv_txt + '/' + txt for txt in sorted(os.listdir(iv_txt))]
# mac has an invisible file .DS_Store
iv_txt_full = [txt for txt in iv_txt_full if 'iv_' in txt]
### all txt files into one df
iv_convert_start = datetime.now().strftime("%Y-%m-%d, %H:%M:%S")
print("The conversion from all .txt documents to one dataframe started at:", iv_convert_start)
#pd.read_csv(iv_txt_full[0], delimiter = "\t", header = None)
iv_DF_raw = [None] * len(iv_txt_full)
for n in range(len(iv_txt_full)):
df_file = pd.read_csv(iv_txt_full[n], delimiter = "\t", header = None)
df_file = df_file.rename(columns = {0:'V'})
df_file['V'] = df_file['V'].str.upper().apply(lambda x: x.strip())
# extract the timestamp substring
df_file['time'] = iv_txt_full[n].split('/')[-1].split('.')[0].split('_')[1]
if df_file['V'].str.contains('GRAND TOTAL', case = False).any() == False:
print(n)
else:
remove = df_file.index >= list(df_file[df_file['V'].str.contains('GRAND TOTAL', case = False)].index)[0]
df_file = df_file.iloc[remove == False]
iv_DF_raw[n] = df_file
iv_DF = pd.concat([df_raw for df_raw in iv_DF_raw]).reset_index(drop = True)
iv_convert_end = datetime.now().strftime("%Y-%m-%d, %H:%M:%S")
print("The conversion ended at:", iv_convert_end)
### CLEANING
iv_DF_archive = iv_DF.copy(deep = True)
iv_DF['V'] = iv_DF['V'].apply(lambda x: x.strip())
iv_DF = iv_DF[iv_DF['V'].str.len() > 1]
# don't forget to escape \
iv_headers = ['PAGE ', 'FOREIGN STATE OF', 'CHARGEABILITY', 'PLACE OF BIRTH', '\(FY 20', '\(FY20','IMMIGRANT VISA']
iv_DF_headers = iv_DF[iv_DF['V'].str.contains('|'.join(iv_headers))]
iv_removed_initial = list(iv_DF_headers.index)
iv_DF = iv_DF[~iv_DF.index.isin(iv_removed_initial)]
# split the column ['V'] for prelimiary output
iv_DF_final = pd.DataFrame()
iv_DF_final['nationality'] = [' '.join(row.split(' ')[:-2]).strip() for row in iv_DF['V']]
iv_DF_final['visa'] = [row.split(' ')[-2].strip() for row in iv_DF['V']]
iv_DF_final['issue'] = [row.split(' ')[-1].strip() for row in iv_DF['V']]
# wrap with list() to remove original index info
iv_DF_final['time'] = list(iv_DF['time'])
# verify that all issue rows are numbers
not_end_num = [row for row in iv_DF_final['issue'] if row[-1] not in [str(num) for num in list(range(0,10,1))]]
not_start_num = [row for row in iv_DF_final['issue'] if row[0] not in [str(num) for num in list(range(0,10,1))]]
if len(not_end_num) == 0 & len(not_start_num) == 0:
print("The issue column looks good.")
else:
print("There are problems in the issue column. Some of them are not numbers.")
# get country list
iv_countries = sorted(list(set(iv_DF_final['nationality'])))
## RESTORE ROWS that contain both data and header/footer
iv_restored_rows = []
for row in iv_DF_headers['V'] + ',' + iv_DF_headers['time']:
if any([x in row for x in iv_countries]):
print(row)
iv_restored_rows.append(row)
# construct a new dataframe with restored rows, same structure that has nationality, visa, issue, and time
iv_restored_df = pd.DataFrame()
iv_restored_df['nationality'] = [' '.join(row.split('IMMIGRANT')[0].strip().split(' ')[:-2]) for row in iv_restored_rows]
iv_restored_df['visa'] = [row.split('IMMIGRANT')[0].strip().split(' ')[-2] for row in iv_restored_rows]
iv_restored_df['issue'] = [row.split('IMMIGRANT')[0].strip().split(' ')[-1] for row in iv_restored_rows]
iv_restored_df['time'] = [row.split(',')[-1].strip() for row in iv_restored_rows]
# concatenate the original and the restored rows
iv_DF_final = pd.concat([iv_DF_final, iv_restored_df],axis = 0).\
drop_duplicates().sort_values(by = ['time','nationality','visa']).\
rename(columns = {'issue':'count'})
iv_DF_final = iv_DF_final.reset_index(drop = True)
not_end_num = [row for row in iv_DF_final['count'] if row[-1] not in [str(num) for num in list(range(0,10,1))]]
not_start_num = [row for row in iv_DF_final['count'] if row[0] not in [str(num) for num in list(range(0,10,1))]]
if iv_DF_final['count'].dtype == 'object':
iv_DF_final['count'] = iv_DF_final['count'].str.replace(',', '')
iv_DF_final['count'] = iv_DF_final['count'].astype('int')
print(iv_DF_final['count'].dtype)
assert iv_DF_final['count'].dtype == "int"
# save IV locally
iv_DF_final.to_csv('iv_alltime.csv', index = False)
#### added 08/02/2023, reduce country/region name variations
print('Before text cleaning, there are', str(len(set(iv_DF_final['nationality']))), 'country/region names.')
iv_DF_final['nationality'] = iv_DF_final['nationality'].str.replace('*','')
iv_DF_final['nationality'] = iv_DF_final['nationality'].str.replace(r'\s+',' ', regex = True)
iv_DF_final['nationality'] = iv_DF_final['nationality'].str.replace(' - ','-')
iv_DF_final['nationality'] = iv_DF_final['nationality'].str.replace(' – ','-')
iv_DF_final['nationality'] = iv_DF_final['nationality'].str.replace('- ','-')
iv_DF_final['nationality'] = iv_DF_final['nationality'].str.replace(' -','-')
print('After text cleaning, there are', str(len(set(iv_DF_final['nationality']))), 'country/region names.')
########################## CONCATENATE IV & NIV, OUTPUT VISA_ALLTIME ############################
iv_DF_final['type'] = 'I'
niv_DF_final['type'] = 'N'
visa_alltime = pd.concat([iv_DF_final, niv_DF_final],axis = 0).sort_values(by = ['time','type','nationality','visa']).reset_index(drop = True)
#### added on 10/19/2024, edit on China, Hong Kong, Macau, Taiwan, United Kingdom
countries = sorted(list(set(visa_alltime['nationality']))) # 1st draft of countries series
china_list = [country for country in countries if "china".upper() and "mainland".upper() in country]
tw_list = [country for country in countries if "taiwan".upper() in country]
hk_list =[country for country in countries if "hong kong".upper() in country]
mc_list=[country for country in countries if "macau".upper() in country or "macao".upper() in country]
uk_list = [country for country in countries if "great britain".upper() in country]
need_rename_list = china_list + tw_list + hk_list + mc_list + uk_list
# must use ["CHINA"], not "CHINA". If doesn't wrap with [], returns the following:
# 'CHINACHINATAIWANTAIWANTAIWANMACAUMACAUMACAU'
renamed_list = ["CHINA"] * len(china_list) + \
["TAIWAN"] * len(tw_list) + ["HONG KONG"] * len(hk_list) + ["MACAU"] * len(mc_list) +\
["UNITED KINGDOM"] * len(uk_list)
rename_dict = dict(zip(need_rename_list, renamed_list))
# iv_DF_final['nationality'] = iv_DF_final['nationality'].map(rename_dict)
#for index, row in visa_alltime.iterrows():
# if 'china'.upper() in row['nationality'] or 'taiwan'.upper() in row['nationality'] or 'hong kong'.upper() in row['nationality'] or 'macau'.upper() in row['nationality']:
# AttributeError: 'str' object has no attribute 'map'
# row['nationality'] = row['nationality'].map(rename_dict)
new_nationality = visa_alltime['nationality'].map(rename_dict).fillna(visa_alltime['nationality'])
visa_alltime['nationality'] = new_nationality
##### OUTPUT COUNTRY LIST #####
countries = sorted(list(set(visa_alltime['nationality']))) # update the countries series
pd.DataFrame(countries).rename(columns = {0: 'country'}).to_csv('countries.csv', index = False)
visa_alltime.to_csv('visa_alltime.csv', index = False)