w3resource

Pandas Series: nsmallest() function

Get the smallest n elements in Pandas

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

Syntax:

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

Parameters:

Name Description Type/Default Value Required / Optional
n Return this many ascending sorted values. int
Default Value: 5
Required
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’
Required

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.

Example:


Download the above Notebook from here.



Inviting useful, relevant, well-written and unique guest posts