1

I have group by data set but I'm unable to convert it to json. It throws out json with a bad format. TO_excel works fine.

Country Sub amount        
        3   source4      
UK      1   source3      
        1   source1      
US      2   source2      

How can I export groupby dataset to_json?

jason
  • 3,932
  • 11
  • 52
  • 123

1 Answers1

2

There is problem you have MultiIndex in DataFrame, so need reset_index:

j = df.reset_index().to_json()
print (j)
{"Country":{"0":"UK","1":"UK","2":"US"},
 "Sub":{"0":1,"1":1,"2":2},
 "amount":{"0":"source3","1":"source1","2":"source2"}}
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252