I have a DataFrame with the following columns and no duplicates:
['region', 'type', 'name', 'value']
that can be seen as a hierarchy as follows
grouped = df.groupby(['region','type', 'name'])
I would like to serialize this hierarchy as a JSON object.
If anyone is interested, the motivation behind this is to eventually put together a visualization like this one which requires a JSON
file.
To do so, I need to convert grouped
into the following:
new_data['children'][i]['name'] = region
new_data['children'][i]['children'][j]['name'] = type
new_data['children'][i]['children'][j]'children'][k]['name'] = name
new_data['children'][i]['children'][j]'children'][k]['size'] = value
...
where region
, type
, name
correspond to different levels of the hierarchy (indexed by i
, j
and k
)
Is there an easy way in Pandas/Python to do this?