如何根据优先级和条件对 pandas df 进行排名?

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

我想根据以下条件对数据框 df.rank(method='dense').astype(int) 进行排名:

  1. 胜利 --> 胜利越多,排名越高`

  2. 输 --> 如果排名相等,那么抽签次数loses/more 次数少的行排名会高 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
    

想法是分别对每一列进行排名,ascending=False 用于 Win,创建 Series 元组并再次 rank

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