Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sort values by a column in a pivot table

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading