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.
DataFrame.nsmallest(n, columns, keep='first')
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 for keep
.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.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'))
pandas
module.df
.df
.nsmallest
method.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.
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'))
pandas
module.df
.df
.Age
column using the nlargest
method with the keep
value as last
.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.