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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/dataframe/dataframe-nsmallest.php