python中的排名算法

use*_*581 0 python algorithm ranking dataframe pandas

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

Athlete A         Athlete  B     Athlete C
speed=10          speed=12       speed=6
endurance=60      endurance=59   endurance=64
Run Code Online (Sandbox Code Playgroud)

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

Mr.*_*. E 5

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

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')
Run Code Online (Sandbox Code Playgroud)

结果:

  Athlete  Endurance  Speed  sorting_column
2       C         64      6            29.2
0       A         60     10            30.0
1       B         59     12            30.8
Run Code Online (Sandbox Code Playgroud)