我有一个pandas dataFrame,包含以下内容:
Athlete A Athlete B Athlete C
speed=10 speed=12 speed=6
endurance=60 endurance=59 endurance=64
我想根据他们的速度和耐力来评定这三位运动员的实力。我想给予耐力稍大一点(0.6)。是否有任何python库可以根据多种条件进行排名?
答案 0 :(得分:4)
您应该使用计算的订单向数据框添加新列,然后按该列对其进行排序。例如:
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