Home > Enterprise >  Reshaping Multi Indexed DF
Reshaping Multi Indexed DF

Time:01-05

I have a dataframe that is structured like so (similar to a pivot table):

A B December 2022 January 2023
A1 B1 100 200
A1 B2 101 201

I'd like to and transpose my dataframe in a way so it reads:

Month A B Value
December 2022 A1 B1 100
December 2022 A1 B2 101
January 2023 A1 B1 200
January 2023 A1 B2 201

etc. I've attempted

df.T

But it gives me:

A A1 A1
B B1 B2
December 2022 100 101
January 2023 200 201

CodePudding user response:

You should use pd.melt:

>>> df.melt(id_vars=['A', 'B'], var_name='Month', value_name='Value')
    A   B          Month  Value
0  A1  B1  December 2022    100
1  A1  B2  December 2022    101
2  A1  B1   January 2023    200
3  A1  B2   January 2023    201

then to reorder columns, you can use this hack:

>>> df.melt(id_vars=['A', 'B'], var_name='Month', value_name='Value') \
      .set_index('Month').reset_index()

           Month   A   B  Value
0  December 2022  A1  B1    100
1  December 2022  A1  B2    101
2   January 2023  A1  B1    200
3   January 2023  A1  B2    201
  • Related