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

Find rank of column relative to other rows

df = pd.DataFrame({'Alice': [4,15,2], 'Bob': [9,3,5], 'Emma': [4,7,19]})

I can find who got the highest score in each round with

df.idxmax(1)
> 0      Bob
  1    Alice
  2     Emma
 dtype: object

But I would like to find in which place Bob finished in each round. Output should be:

> 0
  2
  1

Seems like something with argsort should work, but can’t quite get it.

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

(Here is the same question, but in SQL Server.)

>Solution :

You can use rank:

df.rank(axis=1, method='first', ascending=False)

NB. check the methods to find the one that better suits your need:

How to rank the group of records that have the same value (i.e. ties):

average: average rank of the group
min: lowest rank in the group
max: highest rank in the group
first: ranks assigned in order they appear in the array
dense: like ‘min’, but rank always increases by 1 between groups.

output:

   Alice  Bob  Emma
0    2.0  1.0   3.0
1    1.0  3.0   2.0
2    3.0  2.0   1.0

NB. note that the ranks start as 1, you can add sub(1) to get a rank from 0

df.rank(axis=1, method='first', ascending=False).sub(1).convert_dtypes()

output:

   Alice  Bob  Emma
0      1    0     2
1      0    2     1
2      2    1     0
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