pythonpandasdataframepandas-melt

How do I melt and/or pivot my pandas dataframe in way that forces x-axis titles to become index? (see description for visual)


Say I have a df that looks like this:

Store Name | 8/7 | 8/14 | 8/21 | 8/28 | more dates...
---------------------------------------------
Store 1    | 2.3 | 4.5  | 6.4  | 6.5  | Value..
Store 2    | 5.6 | 6.4  | 5.4  | 4.3  | Value..
Store 3    | 3.4 | 5.7  | 5.3  | 7.8  | Value..

But I want my df to look like this:

Date | Store | Value
----------------------
8/7  |Store 1| 2.3
8/7  |Store 2| 5.6
8/7  |Store 3| 3.4
8/14 |Store 1| 4.5
8/14 |Store 2| 6.4
8/14 |Store 3| 5.7
8/21 |Store 1| 6.4
8/21 |Store 2| 5.4
8/21 |Store 3| 5.3
etc..

How do I do this with pandas? I'm assuming this is a melt function of pandas, but I'm struggling to wrap my head around this one...


Solution

  • Wow, really simply solution actually:

    df.melt(id_vars='Store')