You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pandas如何实现并列分数跳号的标准竞赛排名效果

解决方案

你需要的竞赛排名规则属于间隙跳跃排名,直接使用pandas.Series.rank的method='min'参数即可实现,无需额外自定义逻辑。

完整示例代码

import pandas as pd

d = {'id':list(range(10)), 
     'score':[10,10,8,8,8,5,4,3,2,1]}
df = pd.DataFrame(d)
# 使用method='min'实现竞赛规则排名,转int去除浮点后缀
df['rank'] = df['score'].rank(method='min', ascending=False).astype(int)

运行结果

id  score  rank
0   0     10     1
1   1     10     1
2   2      8     3
3   3      8     3
4   4      8     3
5   5      5     6
6   6      4     7
7   7      3     8
8   8      2     9
9   9      1    10

结果完全匹配需求:

  • 2名10分参赛者并列第1名
  • 3名8分参赛者并列第3名
  • 后续无并列分数依次对应6到10名

参数补充说明

pandas的rank方法常用method参数区别:

  • dense:连续无间隙排名,相同分数排名一致,后续排名仅+1,是你之前用到的逻辑
  • min:跳跃式排名,相同分数取当前位次的最小值,后续排名跳过并列占用的所有位次,完全匹配竞赛排名规则
  • max:相同分数取当前位次的最大值,逻辑和min相反
  • average:默认参数,相同分数取对应位次的平均值

内容的提问来源于stack exchange,提问作者jbuddy_13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 02:45:04