Coding Ref

What is nlargest() in Pandas?

What is nlargest() in Pandas?

The nlargest method in Pandas is used to return the largest n elements from a DataFrame or Series.

This method takes the number n as an argument, and returns a new DataFrame or Series containing the n largest elements from the original DataFrame or Series.

For example, consider the following DataFrame:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 4, 5],
    'B': [10, 20, 30, 40, 50],
    'C': [100, 200, 300, 400, 500]
})

This DataFrame has three columns A, B, and C, with five rows of data.

To get the largest three elements from the B column, you could do the following:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 4, 5],
    'B': [10, 20, 30, 40, 50],
    'C': [100, 200, 300, 400, 500]
})

# Get the largest three elements from column B
largest = df['B'].nlargest(3)

# Print the resulting Series
print(largest)
output
4    50
3    40
2    30
Name: B, dtype: int64

In the code above, the nlargest method is applied to the B column of the DataFrame, and the number 3 is passed as an argument.

This returns a new Series object containing the three largest elements from the original B column.

In this case, the resulting Series has the values 50, 40, 30.

You can also use the nlargest method to get the largest n elements from a DataFrame by specifying the column argument.

For example, if you wanted to get the largest three elements from the entire DataFrame, you could do the following:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 4, 5],
    'B': [10, 20, 30, 40, 50],
    'C': [100, 200, 300, 400, 500]
})

# Get the largest three elements from the DataFrame
largest = df.nlargest(3, columns='B')

# Print the resulting DataFrame
print(largest)

In the code above, the nlargest method is applied to the DataFrame, and the columns argument is used to specify that the B column should be used to determine the largest elements.

The number 3 is also passed as an argument, which specifies that the largest three elements should be returned.

The nlargest method returns a new DataFrame containing the three largest elements from the original DataFrame, based on the values in the B column.

In this case, the resulting DataFrame has three rows with the values:

output
   A   B    C
4  5  50  500
3  4  40  400
2  3  30  300

You'll also like

Related tutorials curated for you

    How to use ewm() in Pandas

    How to groupby, then sort within groups in Pandas

    How to make a crosstab in Pandas

    How to select multiple columns in Pandas

    How to use astype() in Pandas

    How to stack two Pandas DataFrames

    How to add an empty column to a Pandas DataFrame

    How to use nunique() in Pandas

    How to reshape a Pandas DataFrame

    How to use str.contains() in Pandas

    How to get the first row in Pandas

    How to use str.split() in Pandas