Search code examples
pandassortingif-statementrankingrank

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
    

Solution

  • Idea is rank each column separately, with ascending=False for Win, create Series of tuples and rank again:

    a = df.Win.rank(method='dense', ascending=False)
    b = df.Lose.rank(method='dense')
    
    df['Rank'] = pd.Series(zip(a, b), index=df.index).rank(method='dense').astype(int)
    print (df)
        Win  Lose  Draw  Rank
    A1  3.0   5.0   6.0     3
    A2  3.0   4.0   3.0     2
    A3  0.0   6.0   8.0     4
    A4  0.0  10.0   4.0     5
    A5  8.0   2.0   4.0     1