Skip to content
Justice Tech School

nlargest and nsmallest

Get the top or bottom rows by a column.

Example
df = pd.DataFrame({"name": ["Ada", "Kofi", "Amina", "Tunde"], "country": ["NG", "GH", "NG", "NG"], "score": [92, 78, 85, 71]})
print(df.nlargest(2, "score")[["name", "score"]])
print(df.nsmallest(1, "score")["name"].tolist())

Result

    name  score
0    Ada     92
2  Amina     85
['Tunde']

Exercise

Top two.

df.(2, "score")

Quiz Test

What does nsmallest(1, "score") give?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.