2

I have a pandas dataframe with multi-index column headers that looks like this:

enter image description here

I would like it to look like this:

enter image description here

0

2 Answers 2

2

Try stack and swaplevel:

>>> df
            A        B      
            1  2  3  1  2  3
11/12/2021  1  2  3  4  5  6

>>> df.stack(level=0).swaplevel()
              1  2  3
A 11/12/2021  1  2  3
B 11/12/2021  4  5  6
Sign up to request clarification or add additional context in comments.

Comments

1
df = df.unstack(level=0).unstack(level=1)

output:

                 1     2     3
A 2021-12-11  52.3  64.9  86.8
B 2021-12-11  32.3  12.6  14.7

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.