What is the nsmallest method in pandas?
Overview
The nsmallest method is used to return the first n rows in ascending order, with the smallest values in columns. The columns that aren’t provided are also returned, but they’re not used for sorting.
Note: Refer to “What is pandas in Python” to learn more about pandas.
Syntax
DataFrame.nsmallest(n, columns, keep='first')
Parameters
-
n: The number of rows to return. -
columns: The column label to order by. This parameter can be a list specifying multiple columns. -
keep: This parameter says how to handle duplicate values. There are three possible values for this attribute that are as follows:first: This picks the first occurrences of the duplicates. This is the default value forkeep.last: This picks the last occurrences of the duplicates.all: This does not drop any duplicates, even if it involves choosing more than ‘n’ items.
Example 1
import pandas as pddf= pd.DataFrame({'Name':['Dom', 'Celeste', 'Abhi', 'Gaby', 'Rachel', 'Sam'],'Age': [20,25,30,18,25,20]})print("Dataframe---")print(df)print("\n")print("Top 2 youngest people:")print(df.nsmallest(2,'Age'))
Explanation
- Line 1: We import the
pandasmodule. - Line 3: We create a pandas DataFrame called
df. - Line 6: We print
df. - Line 9: We print the top two youngest people using the
nsmallestmethod.
The value of the keep argument of the nsmallest method is by default first. Hence, though there were two records with Age as 20, the first occurring record, meaning 0 Dom 20 is taken into consideration.
Example 2
import pandas as pddf= pd.DataFrame({'Name':['Dom', 'Celeste', 'Abhi', 'Gaby', 'Rachel', 'Sam'],'Age': [20,25,30,18,25,20]})print("Dataframe---")print(df)print("\n")print("Top 2 youngest people:")print(df.nsmallest(2,'Age', keep='last'))
Explanation
- Line 1: We import the
pandasmodule. - Line 3: We create a panda DataFrame called
df. - Line 6: We print
df. - Line 9: We print the top two people in ascending order of the
Agecolumn using thenlargestmethod with thekeepvalue aslast.
Here, the value of the keep argument of the nlargest method is last. Hence, though there were two records with Age as 20, the last occurring record, meaning 5 Sam 20 is taken into consideration.