Reputation: 315
I have a Pandas DataFrame that looks like this:
Date Col1 Col2
0 2000-01-01 Val 1a Val 2a
1 2000-01-02 Val 1b Val 2b
2 2000-01-03 Val 1c Val 2c
3 2000-01-04 Val 1d Val 2d
I need to reshape it, as well as give a name to the column axis so that is looks like this:
Column_Name Col1 Col2
Date
2000-01-01 Val 1a Val 2a
2000-01-02 Val 1b Val 2b
2000-01-03 Val 1c Val 2c
2000-01-04 Val 1d Val 2d
According to the documentation, I'm guessing what I need is something inbetween pivot()
and stack()
, but I can't seem to figure it out exactly.
Upvotes: 1
Views: 157
Reputation: 133640
With your shown samples, could you please try following. Using set_index
and rename_axis
functions from Pandas.
import pandas as pd
df.set_index('Date').rename_axis(columns='Column_name')
Result:
Column_name Col1 Col2
Date
2000-01-01 Val 1a Val 2a
2000-01-02 Val 1b Val 2b
2000-01-03 Val 1c Val 2c
2000-01-04 Val 1d Val 2d
Upvotes: 4