0

I have a simple dataframe:

import pandas as pd

df=pd.DataFrame({"Mode":["Air", "Rail", "Car"], "Grp1":[0.7, 0.2, 0.1], "Grp2":[0.9, 0.05, 0.05]})
df
    Mode    Grp1    Grp2
0   Air     0.7     0.90
1   Rail    0.2     0.05
2   Car     0.1     0.05

What I would like to do is to take the existing "Mode" column in my dataframe and put it as level=1 in the multi-level column structure, so that I would get something like:

Grp1              Grp2
Air  Rail  Car    Air  Rail  Car
0.7   0.2  0.1    0.90 0.05  0.05

Could someone help me with this one please?

Owen
  • 11
  • 2
  • This is question 10 in the dupe. Your solution: `df.pivot_table(index=df.groupby('Mode').cumcount(), columns='Mode', values=['Grp1', 'Grp2'])` – Nick May 25 '23 at 13:09

0 Answers0