What is the nlargest method in pandas?
Overview
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.
Syntax
DataFrame.nlargest(n, columns, keep='first')
Parameters
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 forkeep.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.
Coding example 1
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'))
Explanation
- Line 1: We import the
pandasmodule. - Line 3: We create a pandas data frame called
df. - Line 6: We print the
df. - Line 9: We print the top two people in ascending order of the
Agecolumn using thenlargestmethod.
Output
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.
Coding example 2
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'))
Explanation
- Line 1: We import the
pandasmodule. - Line 3: We create a pandas data frame 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.
Output
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.