1

I have a pandas dataFrame that consist of the following:

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

I would like to rank the strength of those three Athletes based on their speed and endurance. I would like to give a slightly greater weight (0.6) to the endurance. Is there any python library to do rankings based on multiple conditions?

EdChum
  • 376,765
  • 198
  • 813
  • 562
user3841581
  • 2,637
  • 11
  • 47
  • 72
  • 3
    You can just add a column for speed and endurance and then do sum 0.6 * weight + speed and rank on this, please show your efforts – EdChum Mar 10 '16 at 13:35
  • Possible duplicate of [Adding calculated column(s) to a dataframe in pandas](http://stackoverflow.com/questions/12376863/adding-calculated-columns-to-a-dataframe-in-pandas) – Tadhg McDonald-Jensen Mar 10 '16 at 13:41

1 Answers1

6

You should add a new column to your dataframe with the calculated order and then sort it by that column. Example:

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')

Result:

  Athlete  Endurance  Speed  sorting_column
2       C         64      6            29.2
0       A         60     10            30.0
1       B         59     12            30.8
Mr. E
  • 2,070
  • 11
  • 23