How to Find Bottom N Values in pandas DataFrame or Series
To find the bottom
n values in a pandas DataFrame or Series, use the nsmallest(n, columns) method for DataFrames or nsmallest(n) for Series. Alternatively, you can sort the data with sort_values() and then select the first n rows.Syntax
For DataFrame:
df.nsmallest(n, columns): Returns the rows with the smallestnvalues in the specifiedcolumns.df.sort_values(by=columns).head(n): Sorts the DataFrame bycolumnsascending and selects the firstnrows.
For Series:
series.nsmallest(n): Returns the smallestnvalues in the Series.series.sort_values().head(n): Sorts the Series ascending and selects the firstnvalues.
python
df.nsmallest(n, columns) series.nsmallest(n) df.sort_values(by=columns).head(n) series.sort_values().head(n)
Example
This example shows how to find the bottom 3 values in a DataFrame column and in a Series using nsmallest() and sort_values().
python
import pandas as pd # Create a sample DataFrame data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'], 'Score': [88, 92, 79, 93, 85]} df = pd.DataFrame(data) # Find bottom 3 scores using nsmallest bottom_3_nsmallest = df.nsmallest(3, 'Score') # Find bottom 3 scores using sort_values bottom_3_sorted = df.sort_values(by='Score').head(3) # Create a Series scores = pd.Series([88, 92, 79, 93, 85], index=['Alice', 'Bob', 'Charlie', 'David', 'Eva']) # Bottom 3 values in Series bottom_3_series = scores.nsmallest(3) print("Bottom 3 rows in DataFrame using nsmallest:") print(bottom_3_nsmallest) print("\nBottom 3 rows in DataFrame using sort_values:") print(bottom_3_sorted) print("\nBottom 3 values in Series:") print(bottom_3_series)
Output
Bottom 3 rows in DataFrame using nsmallest:
Name Score
2 Charlie 79
4 Eva 85
0 Alice 88
Bottom 3 rows in DataFrame using sort_values:
Name Score
2 Charlie 79
4 Eva 85
0 Alice 88
Bottom 3 values in Series:
Charlie 79
Eva 85
Alice 88
dtype: int64
Common Pitfalls
1. Forgetting to specify the column in nsmallest() for DataFrames: You must provide the column name to find bottom values in that column.
2. Using nsmallest() on DataFrame without columns argument causes error.
3. Confusing nsmallest() with nlargest() which finds top values.
python
import pandas as pd df = pd.DataFrame({'A': [5, 2, 9], 'B': [1, 3, 7]}) # Wrong: missing column argument # df.nsmallest(2) # This will raise TypeError # Right: specify column bottom_2 = df.nsmallest(2, 'A') print(bottom_2)
Output
A B
1 2 3
0 5 1
Quick Reference
df.nsmallest(n, 'col'): Fast way to get bottomnrows by column.df.sort_values('col').head(n): Sort then slice to get bottomn.series.nsmallest(n): Bottomnvalues in Series.series.sort_values().head(n): Sort Series ascending and take firstn.
Key Takeaways
Use df.nsmallest(n, column) to quickly find bottom n rows by a column in a DataFrame.
For Series, use series.nsmallest(n) to get bottom n values easily.
You can also sort with sort_values() and then select the first n rows or values.
Always specify the column name when using nsmallest() on DataFrames.
nsmallest() is more efficient than sorting when you only need a few bottom values.