nlargest()
noun
say it: “EN LAR-jist” /ˈɛn ˈlɑrdʒɪst/
A pandas method that picks the given number of rows with the largest values in a column, biggest first.
What does nlargest() do in pandas?
nlargest() is a pandas method that gives the rows with the largest values in one column, biggest first, such as the three products with the most reviews. nsmallest() gives the smallest.
import pandas as pd
products = pd.DataFrame({"name": ["Trail Boot", "Rain Jacket", "Camp Stove", "Headlamp"],
"reviews": [210, 95, 340, 180]})
top = products.nlargest(3, "reviews")
print(top["name"].tolist()) # ['Camp Stove', 'Trail Boot', 'Headlamp']
In a sentence
- “The report's top-ten table came from nlargest() on the sales column.”
- “nlargest() gave the three most-reviewed products in one line.”
Level: Journeyman · More Python terms at this level
Updated