向现有多索引数据框添加其他索引

时间:2014-04-29 13:07:46

标签: python pandas

假设我有一个多索引数据框:

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

我想为它添加一个额外的索引('day')(来自现有的一组列)

以下不起作用:

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

如何在不重新设置索引的情况下执行此操作?

1 个答案:

答案 0 :(得分:4)

只需设置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]