Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. import pandas as pd
  2. import json
  3.  
  4. df = pd.read_csv("datatable.csv")
  5. jsonfilepath = 'output.json'
  6.  
  7.  
  8. def recur_dictify(frame):
  9.     if len(frame.columns) == 1:
  10.         if frame.values.size == 1:
  11.             return frame.values[0][0]
  12.         return frame.values.squeeze()
  13.     grouped = frame.groupby(frame.columns[0])
  14.     d = {k: recur_dictify(g.iloc[:, 1:]) for k, g in grouped}
  15.     return d
  16.  
  17.  
  18. print(recur_dictify(df))
  19. output = recur_dictify(df)
  20.  
  21.  
  22. with open(jsonfilepath, "w") as jsonFile:
  23.     jsonFile.write(json.dumps(output, indent=4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement