如何防止pandas在JSON转换期间更改多索引DataFrame结构?

时间:2017-11-08 01:35:03

标签: python json pandas dataframe

我最初有以下数据框:

cat   cat    1  1  1  1  1
      dog    0  0  0  0  0
      fox    0  0  0  0  0
      jumps  1  1  1  1  1
      mouse  1  1  1  1  1
      over   1  1  1  1  1
      the    1  1  1  1  1
dog   cat    0  0  0  0  0
      dog    1  1  1  1  1
      fox    1  1  1  1  1
      jumps  1  1  1  1  1
      mouse  0  0  0  0  0
      over   1  1  1  1  1
      the    1  1  1  1  1
fox   cat    0  0  0  0  0
      dog    1  1  1  1  1
      fox    1  1  1  1  1
      jumps  1  1  1  1  1
      mouse  0  0  0  0  0
      over   1  1  1  1  1
      the    1  1  1  1  1
jumps cat    1  1  1  1  1
      dog    1  1  1  1  1
      fox    1  1  1  1  1
      jumps  1  1  1  1  1
      mouse  1  1  1  1  1
      over   1  0  1  1  1
      the    1  0  1  1  1
mouse cat    1  1  1  1  1
      dog    0  0  0  0  0
      fox    0  0  0  0  0
      jumps  1  1  1  1  1
      mouse  1  1  1  1  1
      over   1  1  1  1  1
      the    1  1  1  1  1
over  cat    1  1  1  1  1
      dog    1  1  1  1  1
      fox    1  1  1  1  1
      jumps  1  1  0  1  0
      mouse  1  1  1  1  1
      over   1  1  1  1  1
      the    1  0  1  1  1
the   cat    1  1  1  1  1
      dog    1  1  1  1  1
      fox    1  1  1  1  1
      jumps  1  1  0  1  1
      mouse  1  1  1  1  1
      over   1  1  0  1  0
      the    1  1  1  1  1

然后我使用以下内容将其转换为JSON格式:

JSON = df.reset_index().to_json()

(reset_index()因为你无法直接将multiindex df转换为JSON)

然后我使用以下内容将其恢复为DataFrame()格式:

new_df = pd.read_json(JSON)

但现在当我打印它时,它会显示以下数据框:

0   1  1  1  1  1     cat     cat
1   0  0  0  0  0     cat     dog
10  1  1  1  1  1     dog   jumps
11  0  0  0  0  0     dog   mouse
12  1  1  1  1  1     dog    over
13  1  1  1  1  1     dog     the
14  0  0  0  0  0     fox     cat
15  1  1  1  1  1     fox     dog
16  1  1  1  1  1     fox     fox
17  1  1  1  1  1     fox   jumps
18  0  0  0  0  0     fox   mouse
19  1  1  1  1  1     fox    over
2   0  0  0  0  0     cat     fox
20  1  1  1  1  1     fox     the
21  1  1  1  1  1   jumps     cat
22  1  1  1  1  1   jumps     dog
23  1  1  1  1  1   jumps     fox
24  1  1  1  1  1   jumps   jumps
25  1  1  1  1  1   jumps   mouse
26  1  0  1  1  1   jumps    over
27  1  0  1  1  1   jumps     the
28  1  1  1  1  1   mouse     cat
29  0  0  0  0  0   mouse     dog
3   1  1  1  1  1     cat   jumps
30  0  0  0  0  0   mouse     fox
31  1  1  1  1  1   mouse   jumps
32  1  1  1  1  1   mouse   mouse
33  1  1  1  1  1   mouse    over
34  1  1  1  1  1   mouse     the
35  1  1  1  1  1    over     cat
36  1  1  1  1  1    over     dog
37  1  1  1  1  1    over     fox
38  1  1  0  1  0    over   jumps
39  1  1  1  1  1    over   mouse
4   1  1  1  1  1     cat   mouse
40  1  1  1  1  1    over    over
41  1  0  1  1  1    over     the
42  1  1  1  1  1     the     cat
43  1  1  1  1  1     the     dog
44  1  1  1  1  1     the     fox
45  1  1  0  1  1     the   jumps
46  1  1  1  1  1     the   mouse
47  1  1  0  1  0     the    over
48  1  1  1  1  1     the     the
5   1  1  1  1  1     cat    over
6   1  1  1  1  1     cat     the
7   0  0  0  0  0     dog     cat
8   1  1  1  1  1     dog     dog
9   1  1  1  1  1     dog     fox

正如您所看到的,结构已完全改变。在转换到JSON和从JSON转换过程中,有什么方法可以保持我的Dataframe结构完全相同吗?

1 个答案:

答案 0 :(得分:1)

此处的索引只是添加了sort_index

pd.read_json(JSON).sort_index()