12

Say I have a Multi-index dataframe:

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

I would like to add an additional index ('day') to it (from the existing set of columns)

The following doesn't work:

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

How can I do this without resetting again the index?

1 Answer 1

17

Just set append=True:

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

Example:

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]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.