Tutorials References Exercises Videos Menu
Website Get Certified Pro NEW

Pandas DataFrame nsmallest() Method

❮ DataFrame Reference


Example

Return the 10 rows with the smallest value for "Calories":

In this example we use a .csv file called data.csv

import pandas as pd

df = pd.read_csv('data.csv')

newdf = df.nsmallest(10, "Calories")
Try it Yourself »

Definition and Usage

The nlargest() method returns a specified number of rows, starting at the top after sorting the DataFrame by the smallest value for a specified column.


Syntax

dataframe.nsmallest(n, columns, keep)

Parameters

The keep parameter is a keyword argument.

Parameter Value Description
n   Required, a Number, specifying the number of rows to return
columns   Optional, A String (column label), or a list of column labels, specifying the column(s) to order by
keep 'all'
'first'
'last'
Optional, default 'last', specifying what to do with duplicate rows.

Return Value

A DataFrame with Boolean values.

This method does not change the original DataFrame.


❮ DataFrame Reference