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 a dataframe based on multi-column and drop rows that do not sort

For example, if I had a dataframe that looks like

A B
1 8
4 9
2 5
6 2

Is there a easier way to sort the two rows where Col-A is in ascending order and B in descending order and any row that B cannot be ordered in descending be dropped? The second row should be dropped because 9 is greater than 5.

I am expecting a table that looks like

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

A B
1 8
2 5
6 2

>Solution :

You can sort_values, then compute a cummin, drop the rows that violate the cummin:

tmp = df.sort_values(['A','B'], ascending=[True, False])

m = tmp['B'].le(tmp['B'].cummin())

out = tmp[m]

# or to keep the original DataFrame
out = df.loc[m[m].index]

Output:

   A  B
0  1  8
2  2  5
3  6  2
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