4

I have a dataframe where I would like to turn the data in the (first level of the) index into a column. Practically my df looks like this:

         col1
CoI     
 AK   0     1
      1    31
      2   NaN 
 BB   0     5
      1    31
      2   NaN 

And I would like to turn it into this:

      col1  CoI

   0     1   AK
   1    31   AK
   2   NaN   AK
   0     5   BB
   1    31   BB
   2   NaN   BB

How can I best do this? I think this is a rather basic functionality, but as with many other "basic" pandas things I cannot find info on this anywhere.

Many Thanks,

1 Answer 1

6

df.reset_index(level=0, inplace=True) should do it. See the docs here.

Sign up to request clarification or add additional context in comments.

1 Comment

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.