How to Use nsmallest in pandas to Get Smallest Values
Use
DataFrame.nsmallest(n, columns) to get the n rows with the smallest values in specified columns. It returns a new DataFrame sorted by those columns in ascending order.Syntax
The nsmallest method has this syntax:
n: Number of rows to return with smallest values.columns: Column name or list of columns to sort by.keep: Which duplicates to keep if there are ties. Options: 'first' (default), 'last', or 'all'.
python
DataFrame.nsmallest(n, columns, keep='first')Example
This example shows how to get the 3 rows with the smallest values in the 'Age' column.
python
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'], 'Age': [25, 30, 22, 35, 28], 'Score': [88, 92, 85, 90, 95]} df = pd.DataFrame(data) # Get 3 rows with smallest Age smallest_ages = df.nsmallest(3, 'Age') print(smallest_ages)
Output
Name Age Score
2 Charlie 22 85
0 Alice 25 88
4 Eva 28 95
Common Pitfalls
Common mistakes include:
- Passing a column name that does not exist causes an error.
- Using
nsmalleston multiple columns requires a list of column names, not a single string. - Confusing
nsmallestwithsort_values—nsmallestis faster for selecting top rows.
python
import pandas as pd data = {'A': [3, 1, 2], 'B': [9, 8, 7]} df = pd.DataFrame(data) # Wrong: passing a string instead of list for multiple columns # df.nsmallest(2, 'A,B') # This will raise an error # Right: pass list of columns result = df.nsmallest(2, ['A', 'B']) print(result)
Output
A B
1 1 8
2 2 7
Key Takeaways
Use nsmallest(n, columns) to quickly get rows with smallest values in specified columns.
Pass a single column name as a string or multiple columns as a list to the columns parameter.
The method returns a new DataFrame sorted ascending by those columns.
Use the keep parameter to control which duplicates to keep when values tie.
Avoid passing invalid column names or wrong types to prevent errors.