Signup/Sign In
PUBLISHED ON: MARCH 25, 2021

Pandas DataFrame nlargest() Method

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

The below is the syntax of the DataFrame.nlargest() method.

Syntax

DataFrame.nlargest(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 descending Order

In the below example, the DataFrame.nlargest() method returns the DataFrame of the first 2 rows in descending 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.nlargest(2,'Height'))

once we run the program we will get the following output.


Name Age Height Weight
2 yuvraj 30 175 75
5 Renuka 20 165 65

Example 2: Getting DataFrame in descending Order

The below example is similar to the previous one, the DataFrame.nlargest() method returns the DataFrame of the first 3 rows in descending 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.nlargest(3,'Age'))

once we run the program we will get the following output.


--------The DataFrame is-------
Name Age Height Weight
2 yuvraj 30 175 75
1 yashas 25 160 60
4 Sindu 25 155 55

Example 3: Getting DataFrame in descending Order

When keep='last' , the DataFrame.nlargest() 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.nlargest(2,'Height',keep='last'))

once we run the program we will get the following output.


Name Age Height Weight
2 yuvraj 30 175 75
5 Renuka 20 165 65

Conclusion

In this tutorial, we learned the Python pandas DataFrame.nlargest() method. We learned the syntax, parameters and applied it on the DataFrame to understand the DataFrame.nlargest() 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.