nlargest method in Pandas
In pandas, the nlargest() method are used to retrieve the first n rows from a DataFrame or Series based on the largest values in one or more columns.
Syntax DataFrame.nlargest(n, columns, keep='first')
The function has the following parameters:
- n: Number of items to retrieve.
- columns: Column label(s) to order by.
- keep: Which duplicates to keep ('first', 'last', or 'all').
Example: With DataFrame.
Python
import pandas as pd # Sample DataFrame df = pd.DataFrame({ 'Name': ['Ashish', 'Harsha', 'Chahat', 'Dyna', 'Elina'], 'Score': [85, 92, 88, 95, 90] }) # Top 3 highest scores top_scores = df.nlargest(3, 'Score') print("Top 3 scores:\n", top_scores)
The output of the above code is shown below:

• For Series:
Python
s = pd.Series([10, 40, 30, 20]) s.nlargest(2) # Returns 2 largest values
The output of the above code is shown below:
