Dance Party
Dance Party

Reputation: 3723

Pandas Add Column Index Level to Data Frame

Given the following data frame:

d2=pd.DataFrame({'Item':['y','y','z','x'],
                'other':['aa','bb','cc','dd']})
d2

    Item    other
0   y       aa
1   y       bb
2   z       cc
3   x       dd

I'd like to add a column index level 1 under the existing one (I think) because I want to join this data frame to another that is a multi-index. I don't want to alter the other data frame because I have already written a lot of code assuming its current structure.

Thanks in advance!

Upvotes: 2

Views: 459

Answers (1)

jezrael
jezrael

Reputation: 863481

IIUC you can add parameter append=True to set_index:

print (d2.set_index('Item', append=True))
       other
  Item      
0 y       aa
1 y       bb
2 z       cc
3 x       dd

Upvotes: 3

Related Questions