df.melt(id_vars=["location", "name"], var_name="Date", value_name="Value") location name Date Value 0 A "test" Jan-2010 12 1 B "foo" Jan-2010 18 2 A "test" Feb-2010 20 3 B "foo" Feb-2010 20 4 A "test" March-2010 30 5 B "foo" March-2010 25
Here is what the above code is Doing:
1. We’re using the melt function to reshape the dataframe.
2. We’re specifying the id_vars as the columns that we want to keep.
3. We’re specifying the var_name as the column that we want to use as the identifier for the rows.
4. We’re specifying the value_name as the column that we want to use as the values for the rows.
The result is a dataframe that is much easier to work with.