The nlargest
method is used to return the first n
rows in descending order, with the largest 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.nlargest(n, columns, keep='first')
This method takes the following parameters:
n
: This is the number of rows to return.
columns
: This is the column label to order by. This parameter can be a list specifying multiple columns.
keep
: This parameter handles duplicate values. There are three possible values for this attribute that are as follows:
first
: It picks the first occurrences of the duplicates. This is the default value for keep
.last
: It picks the last occurrences of the duplicates.all
: It does not drop any duplicates, even if it involves choosing more than “n” items.Let’s look at an example below:
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 people by seniority:")print(df.nlargest(2,'Age'))
pandas
module.df
.df
.Age
column using the nlargest
method.The value of the keep
argument of the nlargest
method is by default first
. Hence, though there are two records with Age
as 25
, the first occurring record, 1 Celeste 25
, is considered.
Let’s look at another example:
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 people by seniority:")print(df.nlargest(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 25
, the last occurring record, 4 Rachel 25
, is considerered.