Custom Json Format Output From Pandas Dataframe
I have a Pandas DataFrame like below: ID | Category | Description | Score ----------------------------------- 1 | 1 | Desc 1 | 20.0 2 | 1 | Desc 2 | 30.
Solution 1:
thanks @IanS
I took idea from your code and I used the below snippet to get my output:
cList = []
groupDict = outputDF.groupby('Category').apply(lambda g: g.drop('Category', axis=1).to_dict(orient='records')).to_dict()
for key, value in groupDict.items():
cList.append(dict(name=str(key)), children=value)
finalJSON = dict(name='Category', children=cList)
finalJSON = json.dumps(finalJSON)
print(finalJSON)
Post a Comment for "Custom Json Format Output From Pandas Dataframe"