向现有的多重索引数据框添加额外索引

12 投票
1 回答
10137 浏览
提问于 2025-04-18 04:45

假设我有一个多重索引的数据表:

df = df.set_index(['Year', 'Month', 'Week'])

我想在这个数据表中再加一个索引('day'),这个索引是从现有的列中来的。

下面的做法不行:

df.set_index([df.index, 'day']) 

我该怎么做才能不重新设置索引呢?

1 个回答

16

只需要把 append=True 设置上就可以了:

df.set_index('Day',append=True)

举个例子:

In [31]:

df = pd.DataFrame({'Year':np.random.randn(5), 'Month':np.random.randn(5), 'Day':np.random.randn(5), 'Week':np.random.randn(5), 'Data':np.random.randn(5)})
df
Out[31]:
       Data       Day     Month      Week      Year
0 -0.491396 -0.150413  1.384564  0.576275 -0.212781
1  0.954844  0.513917  0.140124 -0.225570 -0.127357
2 -0.147866  1.093051 -0.709818 -1.453956  0.977121
3 -0.156877  0.252677 -1.045523 -2.242977 -0.313560
4  0.823496  0.671079 -1.181015  0.472536  1.092560

[5 rows x 5 columns]
In [32]:

df = df.set_index(['Year', 'Month', 'Week'])
df
Out[32]:
                                   Data       Day
Year      Month     Week                         
-0.212781  1.384564  0.576275 -0.491396 -0.150413
-0.127357  0.140124 -0.225570  0.954844  0.513917
 0.977121 -0.709818 -1.453956 -0.147866  1.093051
-0.313560 -1.045523 -2.242977 -0.156877  0.252677
 1.092560 -1.181015  0.472536  0.823496  0.671079

[5 rows x 2 columns]
In [33]:

df.set_index('Day',append=True)
Out[33]:
                                             Data
Year      Month     Week      Day                
-0.212781  1.384564  0.576275 -0.150413 -0.491396
-0.127357  0.140124 -0.225570  0.513917  0.954844
 0.977121 -0.709818 -1.453956  1.093051 -0.147866
-0.313560 -1.045523 -2.242977  0.252677 -0.156877
 1.092560 -1.181015  0.472536  0.671079  0.823496

[5 rows x 1 columns]

撰写回答