w3resource

Pandas DataFrame: nsmallest() function

DataFrame - nsmallest() function

The nsmallest() function is used to get the first n rows ordered by columns in ascending order.

This method is equivalent to df.sort_values(columns, ascending=True).head(n), but more performant.

Syntax:

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

Parameters:

Name Description Type/Default Value Required / Optional
n     Number of items to retrieve.
int Required
columns   Column name or names to order by. list or str Required
keep   Where there are duplicate values:
  • first : take the first occurrence.
  • last : take the last occurrence.
  • all : do not drop any duplicates, even it means selecting more than n items.
{‘first’, ‘last’, ‘all’}
Default Value: ‘first’
Required

Returns: DataFrame

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - nlargest() function
Next: DataFrame - stack() function



Share this Tutorial / Exercise on : Facebook and Twitter