Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: MultiIndex Dataframe to json-like list of dictionaries

I want to store this data frame

df = pd.DataFrame({
            'id':[1,1,2,2], 
            'gender':["m","m","f","f"], 
            'val1':[1,2,5,6], 
            'val2':[3,4,7,8]
}).set_index(['id','gender'])

as a json file that contains lists of dictionaries like this:

d = [{'id':1, 'gender':"m", 'val1':[1,2], 'val2':[3,4]},
     {'id':2, 'gender':"f", 'val1':[5,6], 'val2':[7,8]}]

All my attempts to use variations of df.to_dict(orient = '...') or df.to_json(orient = '...') did not produce the desired output.

like image 639
HOSS_JFL Avatar asked Nov 15 '25 11:11

HOSS_JFL


1 Answers

You first need to aggregate as list with groupby.agg and reset_index:

(df.groupby(['id', 'gender']).agg(list)
   .reset_index().to_json(orient='records')
)

Output:

[{"id":1,"gender":"m","val1":[1,2],"val2":[3,4]},{"id":2,"gender":"f","val1":[5,6],"val2":[7,8]}]
like image 195
mozway Avatar answered Nov 17 '25 09:11

mozway



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!