【问题标题】:Ranking algorithms in pythonpython中的排名算法
【发布时间】:2016-03-10 22:10:55
【问题描述】:

我有一个包含以下内容的 pandas 数据框:

Athlete A         Athlete  B     Athlete C
speed=10          speed=12       speed=6
endurance=60      endurance=59   endurance=64

我想根据这三位运动员的速度和耐力对他们的力量进行排名。我想给耐力稍大一点的权重(0.6)。有没有python库可以做多条件排名?

【问题讨论】:

标签: python algorithm pandas dataframe ranking


【解决方案1】:

您应该使用计算的顺序向数据框添加一个新列,然后按该列对其进行排序。示例:

import pandas as pd

df = pd.DataFrame({'Athlete': ['A','B','C'],
                   'Speed': [10,12,6],
                   'Endurance': [60,59,64]})

df['sorting_column'] = df.Speed*0.4 + df.Endurance*0.6 #I think you want 40% speed and 60% endurance right?
df.sort(columns='sorting_column')

结果:

  Athlete  Endurance  Speed  sorting_column
2       C         64      6            29.2
0       A         60     10            30.0
1       B         59     12            30.8

【讨论】:

  • 它有效,但我认为我们可以在制作新专栏之前先将速度和耐力正常化。
猜你喜欢
  • 2015-04-15
  • 2013-06-06
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 2019-10-16
  • 2023-03-25
相关资源
最近更新 更多