2017-04-23 57 views

回答

6

你可以使用orient='split',存儲在列表中的索引和列信息,其中維持秩序:

In [34]: df 
Out[34]: 
    A C B 
5 0 1 2 
4 3 4 5 
3 6 7 8 

In [35]: df.to_json(orient='split') 
Out[35]: '{"columns":["A","C","B"],"index":[5,4,3],"data":[[0,1,2],[3,4,5],[6,7,8]]}' 

In [36]: pd.read_json(df.to_json(orient='split'), orient='split') 
Out[36]: 
    A C B 
5 0 1 2 
4 3 4 5 
3 6 7 8 

只記得閱讀以及使用orient='split',否則你會得到

In [37]: pd.read_json(df.to_json(orient='split')) 
Out[37]: 
    columns  data index 
0  A [0, 1, 2]  5 
1  C [3, 4, 5]  4 
2  B [6, 7, 8]  3