Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import json
- df = pd.read_csv("datatable.csv")
- jsonfilepath = 'output.json'
- def recur_dictify(frame):
- if len(frame.columns) == 1:
- if frame.values.size == 1:
- return frame.values[0][0]
- return frame.values.squeeze()
- grouped = frame.groupby(frame.columns[0])
- d = {k: recur_dictify(g.iloc[:, 1:]) for k, g in grouped}
- return d
- print(recur_dictify(df))
- output = recur_dictify(df)
- with open(jsonfilepath, "w") as jsonFile:
- jsonFile.write(json.dumps(output, indent=4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement