I am trying to create a nested JSON block and came accross this awesome solution Pandas grouping by multiple columns to get a multi nested Json:
test = [df.groupby('cat_a')\
.apply(lambda x: x.groupby('cat_b')\
.apply(lambda x: [x.groupby('cat_c')
.apply(lambda x: x[['participants_actual','participants_registered']].to_dict('r')
).to_dict()]
).to_dict()
).to_dict()]
import json
json_res = list(map(json.dumps, test))
This works well for my usecase. However, as I cannot control the dataframe in all cases, there may be more than the three levels noted here.
I could easily imagine getting the levels as follows:
for c in cols[:-2]:
.... perform level gropuping
However, as each of the lamba and apply functions feeds into the level above, I am not sure how I could write such a statement in a for loop.
Is there a path for make this statement more dynamic?