2

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!

1 Answer 1

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.