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

In pandas, how to differentiate between two equal ranks based on the value of another column?

I have a dataframe df:

Index A B Rank
1 55 27 1.0
2 62 15 2.5
3 76 15 2.5
4 85 04 4.0

Where the 'Rank' column was created by ranking 'B' in descending order. with :

df['Rank'] = df['B'].rank(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

In rows 2 and 3 the ranks assigned are the same. Whenever there are same ranks assigned to two different rows, I want to break the tie by using the value of column 'A' in descending order so that the row containing the higher value of 'A' gets a lower rank.

Desired output:

Index A B Rank
1 55 27 1.0
2 62 15 3.0
3 76 15 2.0
4 85 04 4.0

Edit:
I want to first rank it by 'B' first, and then only use 'A' to decide between the same ranked elements, such that all the other ranks are still decided only by 'B'.

>Solution :

One option using sort_values and :

order = df.sort_values(by=['B', 'A'], ascending=[False, False]).index

df.loc[order, 'Rank'] = np.arange(len(df))+1

A variant combined with rank:

df['Rank'] = (df.sort_values(by=['B', 'A'], ascending=[False, False])['B']
                .rank(method='first', ascending=False)
             )

Output:

   Index   A   B  Rank
0      1  55  27     1
1      2  62  15     3
2      3  76  15     2
3      4  85   4     4
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