Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Module

df.nsmallest

Returns the first n DataFrame rows with the smallest values in one or more columns, ordered ascending. Other columns are included but do not influence ranking.

How it works

Call nsmallest with a row count and one or more ordering columns. It returns the selected rows in ascending order, retaining every DataFrame column; when values tie, keep controls which occurrences are retained.

Syntax

DataFrame.nsmallest(n, columns, keep='first')

Parameters

NameTypeDescription
nintNumber of items to retrieve.
columnslist or strColumn name or names to order by.
keep{'first', 'last', 'all'}, default 'first'Tie behavior: 'first' keeps the first occurrence, 'last' the last occurrence, and 'all' every tie at the boundary; defaults to 'first'.

Returns

DataFrame with the first n rows ordered by columns in ascending order.

Key points

  • The method selects the smallest n rows and orders the result from low to high by the chosen columns.
  • All DataFrame columns remain available in the result, even though only the listed columns determine ranking.
  • It is a concise, faster alternative to sorting ascending and taking the first n rows.
  • The n argument specifies how many rows to request.
  • The columns argument accepts either one column name or multiple names for ordering.
  • The default keep='first' preserves the earliest occurrence among tied values.

Common mistakes

  • Using keep='all' can return more than n rows when the boundary value has ties.
  • With keep='last', duplicate values are selected from the reverse order of appearance.

Example: Select rows with the smallest values

import pandas as pd

df = pd.DataFrame({
    "city": ["A", "B", "C", "D"],
    "population": [120, 80, 80, 200],
    "gdp": [4, 7, 3, 9],
})

result = df.nsmallest(2, ["population", "gdp"], keep="first")
print(result)

Selects the two rows with the smallest population, breaking ties by GDP.

When to use it

Top-k minimum selection

Find the lowest-valued records in a DataFrame without sorting the entire table.

Multi-column ordering

Rank by a primary metric and use additional columns to break ties.

Tie-preserving selection

Retain every record tied at the cutoff rather than forcing exactly n rows.

Interview questions

QWhat does DataFrame.nsmallest do?

DataFrame.nsmallest(n, columns, keep='first') returns the first n rows ordered by the specified columns in ascending order. It includes unspecified columns in the result, but those columns do not affect ordering.

Follow-up: How would you reproduce it without nsmallest? Use df.sort_values(columns, ascending=True).head(n), although the documentation notes nsmallest is more performant.

QHow do n and columns control selection?

n is the number of items to retrieve, while columns is a column name or a list of names used for ordering. Multiple columns apply successive ordering criteria, so later columns resolve ties from earlier ones.

Follow-up: What happens if the ordering columns are omitted? The call is invalid because both n and columns are required positional arguments in the documented signature.

QHow does keep affect ties?

keep determines duplicate handling: first keeps the first occurrence, last keeps the last occurrence, and all keeps every tie at the boundary. Therefore keep='all' may produce more than n rows.

Follow-up: Why might keep='all' be useful? It preserves every row tied at the cutoff instead of arbitrarily dropping tied rows.

  • DataFrame.nlargest
  • DataFrame.sort_values
  • DataFrame.head
  • Series.nsmallest

Source: pandas.DataFrame.nsmallest — pandas 3.0.6 documentation. Checked 2026-10-15.