Skip to content
Advertisement

Pandas: Pivot a DataFrame, columns to rows

I have a DataFrame defined like this:

JavaScript

The DataFrame is now this:

JavaScript

I want to pivot the DataFrame so that it then looks like this:

JavaScript

I think I want to do this via pivoting, but I’ve not yet worked out how to do this using the pivot() or pivot_table()functions. How can I do this, with or without using a pivot?

Advertisement

Answer

You can use melt, but first rename columns by dict:

JavaScript

Or map column day by dict:

JavaScript

Another solution with stack:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement