pandas read json not working on MultiIndex

后端 未结 3 664
后悔当初
后悔当初 2020-12-11 16:25

I\'m trying to read in a dataframe created via df.to_json() via pd.read_json but I\'m getting a ValueError. I think it may have to do

相关标签:
3条回答
  • 2020-12-11 16:55

    This is not implemented ATM, see the issue here: https://github.com/pydata/pandas/issues/4889.

    You can simply reset the index first, e.g

    df.reset_index().to_json(...)
    

    and it will work.

    0 讨论(0)
  • 2020-12-11 17:06

    Or you can just write json with orient = 'table'

    df.to_json(path_or_buf='test.json', orient='table')

    read multi_index json

    pd.read_json('test.json', orient='table')

    0 讨论(0)
  • 2020-12-11 17:11

    if you want to return MultiIndex structure:

     # save MultiIndex indexes names 
     indexes_names = df.index.names
    
     df.reset_index().to_json('dump.json')
    
     # return back MultiIndex structure:
     loaded_df = pd.read_json('dump.json').set_index(indexes_names)
    
    0 讨论(0)
提交回复
热议问题