0

Basic basic question but can't get it right. I'm trying to sort by scores and then get the top name associated with the top score.

import pandas as pd
df = pd.DataFrame({'score' :[1,5,7,999], 'name':['jack','jill','chris','kevin']})
df.sort_values(by= 'score', ascending=False, inplace=True)
df
df.name[0]

However with this approach I get Jack instead of Kevin since it seems to be going by the order the names appeared in the data frame creation. What's the obvious thing I am missing?

3 Answers 3

3

That is because when you access df.name[0], it points to the index of the row (check out by calling df.index). The index value of the row where name is jack is 0, since when you created the df the first instance was jack.

In order to access the first row on the sorted df, use .iloc for positional indexing (or .loc if you want a label-based indexing).

import pandas as pd
df = pd.DataFrame({'score' :[1,5,7,999], 'name':['jack','jill','chris','kevin']})
df.sort_values(by= 'score', ascending=False, inplace=True)
df
df.name.iloc[0]

This returns kevin.

Sign up to request clarification or add additional context in comments.

Comments

0

The index can be restructured by reset_index.

In your case, execute below after sort_values would restructure index.

df.reset_index(drop=True, inplace=True)

Comments

0

Try this .using idxmax

df.loc[df.score.idxmax(),'name']

Out[5631]: 'kevin'

Comments

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.