Pandas DataFrame | nsmallest method
Start your free 7-days trial now!
Pandas DataFrame.nsmallest(~) method returns n rows with the smallest value for the specified columns. The returned rows are sorted in ascending order.
Parameters
1. nlink | int
The number of rows you want returned.
2. columnslink | string or list of strings
The label(s) of the columns you want to order by.
3. keeplink | string | optional
How to handle duplicate values in the edge cases. Edge cases arise, for instance, if you select n=2, and the values are [5,4,4,4].
Value | How to deal with duplicate values at edge cases |
|---|---|
| Keep the first occurrence(s). |
| Keep the last occurrence(s). |
| Keep all the occurrences. |
For "all", the number of returned rows may exceed n. By default, keep="first".
Return Value
A DataFrame with n rows that have the smallest value in the specified columns, in ascending order.
Examples
Basic usage
Consider the following DataFrame:
df = pd.DataFrame({"A":[4,6,1], "B":[3,3,8]})df
A B0 4 31 6 32 1 8
To get the 2 rows with the smallest value for column A:
df.nsmallest(2, "A")
A B2 1 80 4 3
Notice how the returned rows are sorted in ascending order of the values in A.
Dealing with duplicate values
Consider the same df we had above:
df
A B0 4 31 6 32 1 8
Keeping only the first
To get the first occurrence of the row with the smallest value for column B:
df.nsmallest(1, "B") # By default, keep="first"
A B0 4 3
Notice how row 0 was returned, as opposed to row 1, despite the fact that they both had the same value (3) for column B.
Keeping only the last
To get the last occurrence instead, set keep="last":
df.nsmallest(1, "B", keep="last")
A B1 6 3
Keeping all occurrences
To keep both the occurrences, set keep="all":
df.nsmallest(1, "B", keep="all")
A B0 4 31 6 3
Notice how despite the fact that we set n=1, we end up with 2 rows.