-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstruct_prescriptions.py
More file actions
36 lines (30 loc) · 1.04 KB
/
construct_prescriptions.py
File metadata and controls
36 lines (30 loc) · 1.04 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
import pandas as pd
import sys
import os
import json
infile = sys.argv[1]
outfile = sys.argv[2]
df = pd.read_csv(infile, sep = "|", dtype='object')
df['Month'] = df["Incurred Date"].astype(str).str[:6].astype(int)
prescriptions = {}
p = df.loc[:,["Month",
"Drug Code",
"GPI",
"Drug Name",
"Strength",
"Member Zip Code DOS",
"Member County DOS",
"Service Provider ZIP",
"Service Provider County"]]
p.rename(columns = {"Drug Code": "NDC"}, inplace = True)
p.replace({"Unknown": None, "Unkno": None, "Unspecified": None}, inplace = True)
grouped = p.groupby(by = ["NDC"], dropna=False)
for drug, frame in grouped:
if drug in prescriptions.keys():
prescriptions[drug] = pd.concat([prescriptions[drug], frame], ignore_index = True)
else:
prescriptions[drug] = frame.reset_index(drop = True)
for key in prescriptions:
prescriptions[key] = prescriptions[key].to_dict()
with open(outfile, "w") as f:
json.dump(prescriptions, f)