mardi 10 novembre 2020

How to rank pandas df based on priorities and conditions?

df.head()

     Win  Lose  Draw
A1   3.0   5.0   6.0
A2   3.0   4.0   3.0
A3   0.0   6.0   8.0
A4   0.0   10.0  4.0
A5   8.0   2.0   4.0

I want to rank dataframe df.rank(method='dense').astype(int) based on following conditions:

  1. Wins --> More the wins, higher the rank`

  2. Lose --> If ranks are equal, then Row with less number of loses/more number of draws will be higher in rank Output :df_rank`

        Rank
    A1    3  # Has wins equal to A2, but has more losses, so ranked after A2
    A2    2  
    A3    4
    A4    5  # Has most number of losses. So ranked last
    A5    1  #Has most number of wins
    

Aucun commentaire:

Enregistrer un commentaire