1

Sorry for the title as it is confusing. I have a pandas dataframe as below.

date     A    B
jan 1    4    5
jan 2    6    8
...     ...   ...

What I wanted to do is transpose the dataframe (which I know df.T) and also needed the resulting dataframe to look like below

col1   date   value
A      jan 1  4
A      jan 2  6
B      jan 1  5
B      jan 2  8

Please advice and thank you in advance

python_interest
  • 874
  • 1
  • 9
  • 27

1 Answers1

2

Use pd.melt:

print(pd.melt(df, 'date'))

Or use df.melt:

print(df.melt('date'))

They both output:

    date variable  value
0  jan 1        A      4
1  jan 2        A      6
2  jan 1        B      5
3  jan 2        B      8
U13-Forward
  • 69,221
  • 14
  • 89
  • 114