-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCGdata processor.py
More file actions
130 lines (127 loc) · 6.6 KB
/
CGdata processor.py
File metadata and controls
130 lines (127 loc) · 6.6 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
import pandas as pd
import numpy as np
import regex as re
def find_serial_number(string):
all_occurence = re.findall(r"\b\d{5}\b",string)
if len(all_occurence) > 0:
output = all_occurence[0].strip()
else:
return ""
return output
def find_model_type(string):
all_occurence = re.findall(r":(.*?)\(",string)
if len(all_occurence) > 0:
output = all_occurence[0].strip()
else:
return ""
return output
def find_date(string):
pattern = r"(?<!\d)(\d{1,2}-\d{2}|-\d{2})(?=\))"
all_occurence = re.findall(pattern,string)
if len(all_occurence) > 0:
output = all_occurence[0].strip()
else:
return ""
return output
def find_report_number(string):
pattern = r"\(([^)]+)\)\s*:"
all_occurence = re.findall(pattern,string)
if len(all_occurence) > 0:
output = all_occurence[0].strip()
else:
output = ""
pattern_cha = r"([A-Za-z]+)\s*(\d+)"
pattern_num = r"\s+(.*)"
match_cha = re.search(pattern_cha, output)
match_num = re.search(pattern_num, output)
name = ""
number = ""
if match_cha:
name = match_cha.group(1)
if match_num:
number = match_num.group(1)
return output, name, number
def find_acquisition_date(string):
pattern = r"(?i)\b(?:acquired:|acq\.)\s*(\d+)"
all_occurence = re.findall(pattern,string)
if len(all_occurence) > 0:
output = all_occurence[0].strip()
else:
return ""
return output
Raw_data = pd.read_excel("CGdata12925.xlsx")
Raw_data = Raw_data[Raw_data["locomotives"].notna()]
Raw_data = Raw_data.reset_index()
Expanded_data = pd.DataFrame(np.zeros(shape=(0,15)),columns=[
'company name','affiliation','former ID', 'ind',
'city','state','address / directions','Phone #',
'Full Reporting Number','Reporting Mark','Reporting Number','locomotive serial number','locomotive model',
'Manufactured Date(MM/YY)','Acquisition Date'
])
for i in range(len(Raw_data)):
x_string = Raw_data.loc[i,"locomotives"]
if len(x_string) > 0:
x_list = re.split(r"\n", x_string)
xlist_clean = [re.sub(r"\s+", " ", x) for x in x_list] # remove extra spaces
# remove spaces at the beginning and end of each string
xlist_clean = [x.strip() for x in xlist_clean]
if len(xlist_clean) >= 1:
for line in xlist_clean:
row_number = len(Expanded_data)
serial_numbers = find_serial_number(line) # Find all 5-digit number sequences that are engine serial numbers
loco_type = find_model_type(line) # Find the Loco model listed after the ":" and before the "(", exceptions may occur
build_date = find_date(line) # Find the build-date, most likely has a lot of exceptions, pattern is "-/YY" or "M-YY" or "MM-YY", need to be directly before a ")"
report_number, name, number = find_report_number(line) # find string inside "()" just before ":"
acquisition_date = find_acquisition_date(line) # find string starts with"acq." or "Acquired:", including the following number
Expanded_data.loc[row_number,"company name"] = Raw_data.loc[i,"company name"]
Expanded_data.loc[row_number,"affiliation"] = Raw_data.loc[i,"affiliation"]
Expanded_data.loc[row_number,"former ID"] = Raw_data.loc[i,"former ID"]
Expanded_data.loc[row_number,"ind"] = Raw_data.loc[i,"ind"]
Expanded_data.loc[row_number,"city"] = Raw_data.loc[i,"city"]
Expanded_data.loc[row_number,"state"] = Raw_data.loc[i,"state"]
Expanded_data.loc[row_number,"address / directions"] = Raw_data.loc[i,"address / directions"]
Expanded_data.loc[row_number,"Phone #"] = Raw_data.loc[i,"Phone #"]
Expanded_data.loc[row_number,"locomotive serial number"] = serial_numbers
Expanded_data.loc[row_number,"locomotive model"] = loco_type
Expanded_data.loc[row_number,"Manufactured Date(MM/YY)"] = build_date
Expanded_data.loc[row_number,"Full Reporting Number"] = report_number
Expanded_data.loc[row_number,"Reporting Mark"] = name
Expanded_data.loc[row_number,"Reporting Number"] = number
Expanded_data.loc[row_number,"Acquisition Date"] = acquisition_date
cols = ['locomotive serial number','locomotive model','Manufactured Date(MM/YY)']
mask_all_empty = Expanded_data[cols].apply(lambda x: x.isna() | (x.str.strip() == ""),axis = 1).all(axis=1)
Expanded_data = Expanded_data[~mask_all_empty]
Expanded_data.to_csv("Expanded_CGData.csv")
# Full_data = pd.read_csv("CGdata_manual_processed.csv",encoding='cp1252')
# Full_data["Manufactured Date(MM/YY)"] = Full_data["Manufactured Date(MM/YY)"].astype(str)
# Expanded_data = pd.DataFrame(np.zeros(shape=(0,12)),columns=[
# 'company name','affiliation','former ID', 'ind',
# 'city','state','address / directions','Phone #',
# 'locomotive number','locomotive model',
# 'Manufactured Date(MM/YY)','locomotives'
# ])
# for i in range(len(Full_data)):
# #Find total number of ";"s and initiate another loop to iterate all instances
# company_slice = Full_data.loc[i,:]
# number_of_loco = len(company_slice["locomotive number"].rsplit(";"))-1
# for loco in range(number_of_loco):
# loco_slice = pd.DataFrame(np.zeros(shape=(1,12)),columns=[
# 'company name','affiliation','former ID', 'ind',
# 'city','state','address / directions','Phone #',
# 'locomotive number','locomotive model',
# 'Manufactured Date(MM/YY)','locomotives'
# ])
# loco_slice.loc[0,"company name"] = company_slice["company name"]
# loco_slice.loc[0,"affiliation"] = company_slice["affiliation"]
# loco_slice.loc[0,"former ID"] = company_slice["former ID"]
# loco_slice.loc[0,"ind"] = company_slice["ind"]
# loco_slice.loc[0,"city"] = company_slice["city"]
# loco_slice.loc[0,"state"] = company_slice["state"]
# loco_slice.loc[0,"address / directions"] = company_slice["address / directions"]
# loco_slice.loc[0,"Phone #"] = company_slice["Phone #"]
# loco_slice.loc[0,"locomotive number"] = company_slice["locomotive number"].rsplit(";",number_of_loco)[loco]
# loco_slice.loc[0,"locomotive model"] = company_slice["locomotive model"].rsplit(";",number_of_loco)[loco]
# loco_slice.loc[0,"Manufactured Date(MM/YY)"] = company_slice["Manufactured Date(MM/YY)"].rsplit(";",number_of_loco)[loco]
# Expanded_data = pd.concat([Expanded_data,loco_slice],axis=0)
# Expanded_data.to_csv("Expanded_CGData.csv")
# # link_name = link_name.rsplit(".",1)[0]