Creating column of rankings without resetting index

Question:

I have a dataframe:

df = pd.DataFrame([['Jim', 93, 87, 66], ['Bob', 88, 90, 65], ['Joe', 72, 100, 70]], columns=['Name', 'Overall', 'Stopping', 'Playing'])

I want to create a dataframe such that each subject has a ranking out of 3 according to their score on Overall, Stopping, Playing.

This is the desired output:

df_ranked

Name  Overall  Stopping  Playing
 Jim        1         3        2
 Bob        2         1        3
 Joe        3         2        1

I believe this can be done by sorting the dataframe by each column "ascending", resetting the index, then creating column of index values.

Is it possible to approach this another way?

Asked By: code_machine

||

Answers:

You’re looking for pandas.DataFrame.rank :

to_skip = ["Name"] # <- add here more columns, if needed

df_ranked = df.set_index(to_skip).rank(ascending=False, method="min").reset_index()

Output :

print(df_ranked)

  Name  Overall  Stopping  Playing
0  Jim      1.0       3.0      2.0
1  Bob      2.0       2.0      3.0
2  Joe      3.0       1.0      1.0
Answered By: Timeless

Same answer as @Timeless using rank but showing how you can select which columns to rank and editing your df in place instead of creating a new table

#select columns for ranking
rank_cols = ['Overall', 'Stopping', 'Playing']

#rank those columns in reverse order as ints and assign back into the df
df[rank_cols] = df[rank_cols].rank(ascending=False).astype(int)
Answered By: mitoRibo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.