-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_data.py
More file actions
270 lines (248 loc) · 11.4 KB
/
load_data.py
File metadata and controls
270 lines (248 loc) · 11.4 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
import csv
import psycopg2
import config
state_abbr = {'Alabama':'AL','Alaska':'AK','Arizona': 'AZ','Arkansas':'AR','California':'CA','Colorado':'CO','Connecticut':'CT','District of Columbia':'DC','Delaware':'DE','Florida':'FL','Georgia':'GA','Hawaii':'HI','Idaho':'ID','Illinois':'IL','Indiana':'IN','Iowa':'IA','Kansas':'KS','Kentucky':'KY','Louisiana':'LA','Maine':'ME','Maryland':'MD','Massachusetts':'MA','Michigan':'MI','Minnesota':'MN','Mississippi':'MS','Missouri':'MO','Montana':'MT','Nebraska':'NE','Nevada':'NV','New Hampshire':'NH','New Jersey':'NJ','New Mexico':'NM','New York':'NY','North Carolina':'NC','North Dakota':'ND','Ohio':'OH','Oklahoma':'OK','Oregon':'OR','Pennsylvania':'PA','Rhode Island':'RI','South Carolina':'SC','South Dakota':'SD','Tennessee':'TN','Texas':'TX','Utah':'UT','Vermont':'VT','Virginia':'VA','Washington':'WA','West Virginia':'WV','Wisconsin':'WI','Wyoming':'WY','United States':'US'}
# hospital table insert
def hospitalInsert(myDBname):
try:
connection = psycopg2.connect(myDBname)
cursor = connection.cursor()
count = 0
postgres_insert_query = """ INSERT INTO hospital (name, address, city, state, zipcode, county_name) VALUES (%s,%s,%s,%s,%s,%s)"""
for i in range(len(name)):
try:
record_to_insert = (name[i], address1[i], city[i], state[i], ZIP[i], county[i])
cursor.execute(postgres_insert_query, record_to_insert)
connection.commit()
count += 1
except(Exception, psycopg2.Error) as error:
print(error)
connection.rollback()
print (str(count) + " record(s) inserted successfully into hospital table")
except (Exception, psycopg2.Error) as error :
print("Failed to insert record into hospital table", error)
# measurement table insert
def measurementInsert(myDBname):
try:
connection = psycopg2.connect(myDBname)
cursor = connection.cursor()
count = 0
# measurement insert
postgres_insert_query = """ INSERT INTO measurement (hospital_name, address, measure_name, measure_id, denominator, score, lower_estimate, higher_estimate, start_date, end_date) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""
for i in range(len(hosp_name)):
try:
record_to_insert = (hosp_name[i], address2[i], measure_name[i], measure_id[i], denominator[i], score[i], le[i], he[i], sd[i], ed[i])
cursor.execute(postgres_insert_query, record_to_insert)
connection.commit()
count += 1
except(Exception, psycopg2.Error) as error:
#print(error)
connection.rollback()
print (str(count) + " record(s) inserted successfully into measurement table")
except (Exception, psycopg2.Error) as error :
print("Failed to insert record into measurement table", error)
# state table insert
def stateInsert(myDBname):
try:
connection = psycopg2.connect(myDBname)
cursor = connection.cursor()
count = 0
# measurement insert
postgres_insert_query = """ INSERT INTO state (name, measure_name, num_of_hosp_worse, num_of_hosp_same, num_of_hosp_better) VALUES (%s,%s,%s,%s,%s)"""
for i in range(len(state_name1)):
try:
record_to_insert = (state_name1[i], mea_name[i], num_worse[i], num_same[i], num_better[i])
cursor.execute(postgres_insert_query, record_to_insert)
connection.commit()
count += 1
except(Exception, psycopg2.Error) as error:
print(error)
connection.rollback()
print (str(count) + " record(s) inserted successfully into state table")
except (Exception, psycopg2.Error) as error :
print("Failed to insert record into state table", error)
# death table insert
def deathInsert(myDBname):
try:
connection = psycopg2.connect(myDBname)
cursor = connection.cursor()
count = 0
# measurement insert
postgres_insert_query = """ INSERT INTO death (cause_name, year, deaths, age_adjusted_death_rate, state_name) VALUES (%s,%s,%s,%s,%s)"""
for i in range(len(cause_name)):
try:
record_to_insert = (cause_name[i], year[i], deaths[i], age_adj_death_rate[i], state_name2[i])
cursor.execute(postgres_insert_query, record_to_insert)
connection.commit()
count += 1
except(Exception, psycopg2.Error) as error:
print(error)
connection.rollback()
print (str(count) + " records inserted successfully into death table")
except (Exception, psycopg2.Error) as error :
print("Failed to insert record into death table", error)
with open('Hospital General Information.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
name, address1, city, state, ZIP, county = [], [], [], [], [], []
for row in csv_reader:
if line_count == 0:
for i in range(len(row)):
if (row[i] == "Hospital Name"):
nameID = i
elif (row[i] == "Address"):
addressID = i
elif (row[i] == "City"):
cityID = i
elif (row[i] == "State"):
stateID = i
elif (row[i] == "ZIP Code"):
ZIPID = i
elif (row[i] == "County Name"):
countyID = i
else:
for i in range(len(row)):
if (i == nameID):
name.append(row[i])
elif (i == addressID):
address1.append(row[i])
elif (i == cityID):
city.append(row[i])
elif (i == stateID):
state.append(row[i])
elif (i == ZIPID):
ZIP.append(row[i])
elif (i == countyID):
county.append(row[i])
line_count += 1
with open('Complications and Deaths - Hospital.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
hosp_name, address2, measure_name, measure_id, denominator, score, le, he, sd, ed= [], [], [], [], [], [], [], [], [], []
for row in csv_reader:
if line_count == 0:
for i in range(len(row)):
if (row[i] == "Hospital Name"):
n1 = i
elif (row[i] == "Address"):
n2 = i
elif (row[i] == "Measure Name"):
n3 = i
elif (row[i] == "Measure ID"):
n4 = i
elif (row[i] == "Denominator"):
n5 = i
elif (row[i] == "Score"):
n6 = i
elif (row[i] == "Lower Estimate"):
n7 = i
elif (row[i] == "Higher Estimate"):
n8 = i
elif (row[i] == "Measure Start Date"):
n9 = i
elif (row[i] == "Measure End Date"):
n10 = i
else:
for i in range(len(row)):
if (row[i] == "Not Available"):
row[i] = -1
if (i == n1):
hosp_name.append(row[i])
elif (i == n2):
address2.append(row[i])
elif (i == n3):
measure_name.append(row[i])
elif (i == n4):
measure_id.append(row[i])
elif (i == n5):
denominator.append(row[i])
elif (i == n6):
score.append(row[i])
elif (i == n7):
le.append(row[i])
elif (i == n8):
he.append(row[i])
elif (i == n9):
sd.append(row[i])
elif (i == n10):
ed.append(row[i])
line_count += 1
with open('Complications and Deaths - State.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
state_name1, mea_name, num_worse, num_same, num_better, start_date2, end_date2 = [], [], [], [], [], [], []
for row in csv_reader:
if line_count == 0:
for i in range(len(row)):
if (row[i] == "Not Available"):
row[i] = -1
if (row[i] == "State"):
n1 = i
elif (row[i] == "Measure Name"):
n2 = i
elif (row[i] == "Number of Hospitals Worse"):
n3 = i
elif (row[i] == "Number of Hospitals Same"):
n4 = i
elif (row[i] == "Number of Hospitals Better"):
n5 = i
elif (row[i] == "Measure Start Date"):
n6 = i
elif (row[i] == "Measure End Date"):
n7 = i
else:
for i in range(len(row)):
if (row[i] == "Not Available"):
row[i] = -1
if (i == n1):
state_name1.append(row[i])
elif (i == n2):
mea_name.append(row[i])
elif (i == n3):
num_worse.append(row[i])
elif (i == n4):
num_same.append(row[i])
elif (i == n5):
num_better.append(row[i])
elif (i == n6):
start_date2.append(row[i])
elif (i == n7):
end_date2.append(row[i])
line_count += 1
with open('NCHS_-_Leading_Causes_of_Death__United_States.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
cause_name, year, deaths, age_adj_death_rate, state_name2 = [], [], [], [], []
for row in csv_reader:
if line_count == 0:
for i in range(len(row)):
if (row[i] == "Cause Name"):
n1 = i
elif (row[i] == "Year"):
n2 = i
elif (row[i] == "Deaths"):
n3 = i
elif (row[i] == "Age-adjusted Death Rate"):
n4 = i
elif (row[i] == "State"):
n5 = i
else:
for i in range(len(row)):
if (i == n1):
cause_name.append(row[i])
elif (i == n2):
year.append(row[i])
elif (i == n3):
deaths.append(row[i])
elif (i == n4):
age_adj_death_rate.append(row[i])
elif (i == n5):
state_name2.append(state_abbr[row[i]])
line_count += 1
# *** create your own database named as whatever you want ***
# *** then run the query "schema.sql" in your data base ***
database_name = "host='localhost' dbname='hospital'" # change the var name to your database name
hospitalInsert(database_name)
measurementInsert(database_name)
stateInsert(database_name)
deathInsert(database_name)