pandas · difficulty ◆
pandas nlargest() — grab the top n rows in one call
Top performers, biggest orders, worst offenders — the n rows you actually wanted from sort_values().head().
Your report needs the top 3 cities, not 1,000 rows sorted — yet most people still sort the whole table to get there.
fully AI-generatedThis text is fully AI-generated and was published without human editorial review.$ df.nlargest()- 0.17.0
- added in pandas
- 3
- signature parameters
- ~2.3x
- speedup vs sort+head
September 2015 (GH 12797)
n, columns, keep — verified against pandas 3.0.3
measured on this machine: 100k rows, top 5, pandas 3.0.3
What it does
df.nlargest(n, 'sales') returns the n rows with the largest values in the column you name, sorted descending — pandas' version of SELECT ... ORDER BY sales DESC LIMIT n. It skips NaN values by default, treats ties the way SQL does (keep='first' keeps the earlier row), and nsmallest() is its mirror for the n smallest rows. Because it returns rows, not just values, you can grab the winning city names straight from the result with ['city'].
Why it matters
The daily questions of business reporting are superlatives: which products sold most, which regions underperformed, which transactions were biggest. The sort_values().head() idiom works but pays full price — a complete O(n log n) sort of every row just to keep five of them. nlargest() exists because that trade bothered pandas' maintainers enough to add a dedicated method (GH 12797, pandas 0.17.0, September 2015): I measured 2.3x faster on 100k rows in pandas 3.0.3, because internally it is a quickselect partition, not a full sort.
Example
$ import pandas as pd
df = pd.DataFrame({
"city": ["Berlin", "Munich", "Hamburg", "Cologne", "Dresden", "Leipzig"],
"region": ["east", "south", "north", "west", "east", "east"],
"sales": [4200, 5100, 3800, 6400, 2900, 3300],
})
df.nlargest(3, "sales") city region sales
3 Cologne west 6400
1 Munich south 5100
0 Berlin east 4200The daily-driver move: the top 3 cities by sales, ranked rows included, in one call.
$ orders = pd.DataFrame({
"order_id": [1001, 1002, 1003, 1004, 1005],
"city": ["Berlin", "Munich", "Hamburg", "Cologne", "Dresden"],
"amount": [250, 40, 980, 120, 30],
})
orders.nlargest(3, "amount") order_id city amount
2 1003 Hamburg 980
0 1001 Berlin 250
3 1004 Cologne 120The audit-style pattern: pull the three biggest orders for review, keeping every column.
$ worst = df.nsmallest(2, "sales")["city"].tolist()
worst['Dresden', 'Leipzig']nsmallest() is the mirror twin — same signature, same rules, n lowest rows.
$ df.nlargest(2, "sales", keep="all") city sales
3 Cologne 6400
4 Dresden 6400Ties handled SQL-style: keep='first' would keep only Cologne — 'all' keeps every row tied at the cutoff.
Common flags
- df.nlargest(n, 'col')
- the core call: n biggest rows by one column, returned sorted descending
- df.nsmallest(n, 'col')
- mirror twin: n smallest rows, same signature, same tie rules
- df.nlargest(n, 'col', keep='all')
- keep every row tied at the cutoff, even if that means more than n rows back
- df.nlargest(n, 'col', keep='last')
- on ties keep the later row instead of the first
- df.nlargest(5, ['sales', 'units'])
- multi-column tie-break: sort by the second column when the first ties
- df.groupby('region')['sales'].nlargest(1)
- top row per group — the pattern behind most leaderboard reports
History
Added in pandas 0.17.0 (September 2015)
nlargest() and nsmallest() arrived together in 0.17.0 (September 2015, GH 12797), with keep='first' as default and the n=5 default on Series. The 0.17.0 changelog also documents the design guard: a DataFrame call without the columns argument raises ValueError, because 'which column?' is a question pandas refuses to guess at.
Design note: the tie rule
keep='first' | 'last' | 'all' was in the method from the start — same options as drop_duplicates(). The rule mirrors SQL's ORDER BY + LIMIT semantics: without keep='all', a tie at the cutoff silently drops rows, which is exactly the behavior reports want when they say 'top 3' but the one that surprises people in code review.
Under the hood
Under the hood: quickselect, not a sort
Internally, DataFrame.nlargest() splits the frame into numeric and non-numeric columns, and for the numeric part builds a numpy array and runs np.argpartition — the quickselect algorithm that guarantees the top n are on one side of a pivot in O(n) average time. It then re-sorts just those n rows. That is where the ~2.3x measured speedup comes from: sort_values() pays O(n log n) over every row; nlargest() pays O(n) plus an n-sized sort. The non-numeric columns are stitched back on with take() using the winning row positions.
Fun facts
Pros & cons
pros
- + Reads like the question you are asking: 'the n largest by this column', one call, no head()
- + Quickselect under the hood — measurably faster than sorting the whole frame (2.3x measured on 100k rows)
- + nsmallest() is the exact mirror — learn one, get both
cons
- − Ties silently drop rows by default — keep='all' is opt-in and may return more than n rows
- − DataFrame calls require the columns argument; omitting it raises ValueError
- − The result is not guaranteed to keep ascending index order — it is sorted by the ranking column, descending
Takeaways
- 1df.nlargest(3, 'sales') is the one-call answer to 'top 3 by sales' — no sort_values().head() detour
- 2nsmallest() is the mirror twin, same signature — worst offenders in one call too
- 3Ties drop rows silently with default keep='first'; use keep='all' when a ranking must be exhaustive
- 4df.groupby('region')['sales'].nlargest(1) gives a per-group leaderboard in one chain
- 5When you need all rows sorted anyway (export, chart), sort_values() is the right tool — nlargest() earns its keep only for top-k
Related commands
archive entry — written and published by a scheduled job, never editorially reviewed. Kept for reference: out of the site’s search index, and no ads run here. the reviewed library →
