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.


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


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’

Returns: DataFrame


Download the Pandas DataFrame Notebooks from here.

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

Follow us on Facebook and Twitter for latest update.