Sorting and Top N
Ordering a table, and taking the few rows that matter.
sort_values returns a new table
by names the column, ascending chooses the direction, and the original is untouched.
import pandas as pd
df = pd.DataFrame({"name": ["Ada", "Alan"], "score": [92, 79]})
print(df.sort_values("score", ascending=False)["name"].tolist())
nlargest says what you meant
Sorting the whole table to look at three rows is more work than the question needs, and nlargest reads better besides.
import pandas as pd
df = pd.DataFrame({"name": ["Ada", "Grace", "Alan"], "score": [92, 88, 79]})
print(df.nlargest(2, "score")["name"].tolist())
Exercise
Try It YourselfWrite top_names(df, n) returning the names of the n highest scorers, best first.
Press Run to see output