基于其他列进行排名的列,但类似于决胜局

问题描述 投票:0回答:1

我有一个数据框:

df = pd.DataFrame({
    "DScore": [2, 2, 3, 4, 5],
    "EScore": [6, 7, 9, 9, 10],
    "Total Score": [17, 15, 15, 23, 25]
})

我想编写代码来创建一个排名列,其中包含基于“总分”列的表中行的分类。如果这些值相等 - 您应该注意 EScore 分数的值,如果它们相等,那么它将基于 DScore 列中的值,如果这些值也相同equal - 我们将为它们分配相同的值。预期结果:

df = pd.DataFrame({
    "DScore": [2, 2, 4, 4, 5],
    "EScore": [6, 7, 9, 9, 10],
    "Total Score": [17, 15, 23, 23, 25],
    "Rank": [3,4,2,2,1]
})
python pandas dataframe rank
1个回答
0
投票

排名与

method=dense
ascending=False

df['Rank'] = df['Total Score'].add(df['EScore'].mul(0.01)).add(df['DScore'].mul(0.0001)).rank(ascending=False, method='dense').astype('int')

df

   DScore  EScore  Total Score  Rank
0       2       6           17     3
1       2       7           15     4
2       4       9           23     2
3       4       9           23     2
4       5      10           25     1
© www.soinside.com 2019 - 2024. All rights reserved.