Home > Net >  How to sort values by a column in a pivot table
How to sort values by a column in a pivot table

Time:01-12

Could you please help me with sorting the values by a column in a pivot table in Python?

I've tried to sort the values in a dataframe before I created the pivot table, but, unfortunately, it didn't help me.

So, here I have the sorted data by date column and ascending=False:

enter image description here

Then I created a pivot table, and here is the result:

enter image description here

Please, help me to sort my pivot table by a date column where ascending=False.

CodePudding user response:

Remove [] from index, columns, values for remove MultiIndex and then sorting columns by DataFrame.sort_index:

df = (alert_.pivot_table(index='col1', columns='col2', values='col3')
            .sort_index(axis=1, ascending=False))
  •  Tags:  
  • Related