How to Use nlargest in pandas to Get Top Values
Use
DataFrame.nlargest(n, columns) to get the top n rows ordered by the specified columns. It returns a new DataFrame with the largest values sorted descending by default.Syntax
The nlargest method selects the top n rows ordered by one or more columns.
n: Number of rows to return.columns: Column name or list of column names to sort by.keep: Which duplicates to keep ('first', 'last', or 'all'). Default is 'first'.
python
DataFrame.nlargest(n, columns, keep='first')Example
This example shows how to get the top 3 rows with the highest values in the 'score' column.
python
import pandas as pd data = {'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'], 'score': [85, 92, 88, 91, 87]} df = pd.DataFrame(data) # Get top 3 rows by 'score' top_scores = df.nlargest(3, 'score') print(top_scores)
Output
name score
1 Bob 92
3 David 91
2 Charlie 88
Common Pitfalls
Common mistakes include:
- Using
nlargeston columns with non-numeric data will cause errors. - Passing multiple columns requires a list, not a string.
- Confusing
nlargestwithsort_valueswhich sorts all rows.
python
import pandas as pd data = {'name': ['Alice', 'Bob', 'Charlie'], 'score': [85, 92, 88]} df = pd.DataFrame(data) # Wrong: passing string instead of list for multiple columns # df.nlargest(2, 'name') # Works but not meaningful for strings # Correct: use list for multiple columns # df.nlargest(2, ['score']) # Works fine # Wrong: using nlargest on non-numeric column # df.nlargest(2, 'name') # Raises TypeError
Quick Reference
Summary tips for using nlargest:
- Use for numeric columns only.
- Specify
nas the number of top rows you want. - Pass column name as string or list of strings.
- Use
keep='all'to keep all duplicates.
Key Takeaways
Use DataFrame.nlargest(n, columns) to get top n rows by column values.
Only numeric columns work correctly with nlargest.
Pass column names as string or list for multiple columns.
Use keep parameter to control duplicate handling.
nlargest returns rows sorted descending by the specified columns.