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)
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'
.