Signup/Sign In

Pandas DataFrame nsmallest() Method

In this tutorial, we will learn the Python Pandas DataFrame.nsmallest() method. This method is used to get the first n rows of the DataFrame which is ordered by columns in ascending order. This method returns the first n rows with the smallest values in columns, in ascending order. The columns that are not specified are returned as well, but not used for ordering.

The below shows the syntax of the DataFrame.nsmallest() method

Syntax

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

Parameters

n: It specifies the int that is the number of rows to return.

columns: It represents the label or list of labels that is the name of the Columns to order by.

keep: It includes ‘first’, ‘last’, ‘all’ and the default is ‘first’

Where there are duplicate values:

  • first: prioritize the first occurrence(s)

  • last: prioritize the last occurrence(s)

  • all:do not drop any duplicates, even it means selecting more than n items.

Example 1: Getting DataFrame in ascending in Pandas

In the below example, the DataFrame.nsmallest() method returns the DataFrame of the first 2 rows in the ascending order by the 'Height' column.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df= pd.DataFrame({'Name':['Chetan','yashas','yuvraj','Pooja','Sindu','Renuka'],'Age':  [20,25,30,18,25,20],'Height': [155,160,175,145,155,165],'Weight': [75,60,75,45,55,65]})
print(df.nsmallest(2,'Height'))


Name Age Height Weight
3 Pooja 18 145 45
0 Chetan 20 155 75

Example 2: Getting DataFrame in ascending in Pandas

The below example is similar to the previous one, the DataFrame.nsmallest() method returns the DataFrame of the first 3 rows in the ascending order by the 'Age' column.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df= pd.DataFrame({'Name':['Chetan','yashas','yuvraj','Pooja','Sindu','Renuka'],'Age':  [20,25,30,18,25,20],'Height': [155,160,175,145,155,165],'Weight': [75,60,75,45,55,65]})
print("--------The DataFrame is-------")
print(df.nsmallest(3,'Age'))


--------The DataFrame is-------
Name Age Height Weight
3 Pooja 18 145 45
0 Chetan 20 155 75
5 Renuka 20 165 65

Example 3: Getting DataFrame in ascending in Pandas

When we use keep='last' , the DataFrame.nsmallest() method prioritizes the last occurrences of the specified column and returns the DataFrame.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df= pd.DataFrame({'Name':['Chetan','yashas','yuvraj','Pooja','Sindu','Renuka'],'Age':  [20,25,30,18,25,20],'Height': [155,160,175,145,155,165],'Weight': [75,60,75,45,55,65]})
print(df.nsmallest(2,'Height',keep='last'))


Name Age Height Weight
3 Pooja 18 145 45
4 Sindu 25 155 55

Conclusion

In this tutorial, we learned the Python pandas DataFrame.nsmallest() method. We learned the syntax, parameters and by applying this method on the DataFrame we solved examples and understood the DataFrame.nsmallest() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.