Skip to content
Advertisement

How to create a new index level with columns name in Pandas?

I would like to add a new level to the index of a dataframe, based on columns name. How can i do that ?

JavaScript

Desired result

JavaScript

I can build the multiindex with this but how can i create a new dataframe and map the data ?

JavaScript

Advertisement

Answer

You can use df.stack()

JavaScript

Back to your code, you can try numpy.ravel to flatten the df.values to match the idx shape (idx has 6 items, df.values only return rows which are 3)

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