Pandas Series: nsmallest() function

Get the smallest n elements in Pandas

The nsmallest() function is used to get the smallest n elements.


Series.nsmallest(self, n=5, keep='first')
Pandas Series nsmallest image


Name Description Type/Default Value Required / Optional
n Return this many ascending sorted values. int
Default Value: 5
keep When there are duplicate values that cannot all fit in a Series of n elements:
  • first : return the first n occurrences in order of appearance.
  • last : return the last n occurrences in reverse order of appearance.
  • all : keep all occurrences. This can result in a Series of size larger than n.
{‘first’, ‘last’, ‘all’}
Default Value: ‘first’

Returns: The n smallest values in the Series, sorted in increasing order.

Notes: Faster than .sort_values().head(n) for small n relative to the size of the Series object.


Download the Pandas Series Notebooks from here.

Previous: Get the largest n elements in Pandas
Next: Percentage change between the current and a prior element