This repository has been archived by the owner on Feb 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkdb.py
56 lines (45 loc) · 1.8 KB
/
mkdb.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
import pandas as pd
import sqlalchemy
import csv
df = pd.read_csv('data/PFALL15.txt', skipfooter=4)
df.drop(['2015', '9', ' ' ,' ' ,'A' ,'0000000.00.3' ,'0' ,'0000000.00.2' ,'0.1' ,'0000000.00', '0000000.00.1'], \
inplace=True, axis=1)
cols = ['carrier', 'locality', 'hcpcs', 'nonFacFee', 'facFee']
df.to_csv('data/PFALL15_new.csv', index=False, header=cols)
df = pd.read_csv('data/PFALL15_new.csv')
carriers = []
with open('data/PFALL15.txt', 'rb') as csvfile:
rdr = csv.reader(csvfile, delimiter=' ', quotechar='|')
for i, row in enumerate(rdr):
if i == 789660: # ignore footer
break
r = ', '.join(row)[8:18]
r = r[:5] + r[8:10]
carriers.append(r)
# off by one hack
carriers = carriers[1:]
del df['carrier']
df['carrier'] = carriers
lookup = []
with open('data/PF15PA.csv', 'rb') as csvfile:
for row in csvfile:
lookup.append(row.strip().split(','))
lookup = {carrier[0]: carrier[1:] for carrier in lookup}
df['state'] = df['carrier'].map(lambda x: lookup[x][0])
df['location'] = df['carrier'].map(lambda x: lookup[x][1])
localities = []
with open('data/PFALL15.txt', 'rb') as csvfile:
rdr = csv.reader(csvfile, delimiter=' ', quotechar='|')
for i, row in enumerate(rdr):
if i == 789660:
break
r = ', '.join(row)[16:18]
localities.append(r)
localities = localities[1:]
df['locality'] = localities
engine = sqlalchemy.create_engine('sqlite:///healthbase.db')
types = {'carrier': sqlalchemy.types.String, 'hcpcs': sqlalchemy.types.String, \
'locality': sqlalchemy.types.String, 'facFee': sqlalchemy.types.Float, \
'nonFacFee': sqlalchemy.types.Float, 'state': sqlalchemy.types.String, \
'location': sqlalchemy.types.String}
df.to_sql("h", engine, if_exists='replace', dtype=types)