Skip to content
Advertisement

reorder data in pandas pivot_table function

I’ve a sample dataframe

JavaScript

I’m trying to pivot the data using

JavaScript

The values are not in order which I’ve mentioned in the above snippet.

How can I re-structure my data to (by also repeating the row labels)

JavaScript

Advertisement

Answer

Use DataFrame.swaplevel with DataFrame.reindex:

JavaScript

EDIT:

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