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?

2
  • 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 Commented Mar 10, 2016 at 13:35
  • Possible duplicate of Adding calculated column(s) to a dataframe in pandas Commented Mar 10, 2016 at 13:41

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

It works, but I think may be we can normalize speed and endurance first before making the new column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.