I have a pandas dataframe with multi-index column headers that looks like this:
I would like it to look like this:
CodePudding user response:
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
CodePudding user response:
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