1

I have a dataframe with this format.

I have this dataframe:

   id    2005-01-07   2008-01-07    ...
0  1Y           1.0          1.6
1  5Y           1.0          1.7
2  6Y           6.0          1.0
3  10Y          2.0          7.1
4  30Y          5.5          8.6

And I would like to convert it in:

   id           Date     number
0  1Y     2005-01-07        1.0
1  1Y     2008-01-07        1.6
2  5Y     2005-01-07        1.0
3  5Y     2008-01-07        1.7
4  6Y     2005-01-07        6.0
5  6Y     2008-01-07        1.0
6  10Y    2005-01-07        2.0
7  10Y    2008-01-07        7.1
8  30Y    2005-01-07        5.5
9  30Y    2008-01-07        8.6
...

Is it possible to achieve this output in python? I tried to use transpose() and replace but can't reach the desired output

Answer by @mechanical_meat:

df = pd.melt(df, id_vars=['id'], var_name='Date', value_name='number')

eduardo2111
  • 339
  • 3
  • 14

1 Answers1

3

Using pd.melt we can go from wide- to long-form in one easy step:

df = pd.melt(df, id_vars=['id'], var_name='Date', value_name='number')

The reason .melt() is of particular use here is that you're unpivoting instead of pivoting:

  • In other words, the remaining columns not identified in id_vars keyword argument are then unpivoted into rows.

If you ever need to go back to the original format you can then use .pivot():

df.pivot(values='number',index='id',columns='Date')
smci
  • 29,564
  • 18
  • 109
  • 144
mechanical_meat
  • 155,494
  • 24
  • 217
  • 209