Pandas Series: nlargest() function
Get the largest n elements in Pandas
The nlargest() function is used to get the largest n elements.
Syntax:
Series.nlargest(self, n=5, keep='first')
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
n | Return this many descending sorted values. | int Default Value: 5 |
Required |
keep | When there are duplicate values that cannot all fit in a Series of n elements:
|
{‘first’, ‘last’, ‘all’} Default Value: ‘first’ |
Required |
Returns: Series
The n largest values in the Series, sorted in decreasing order.
Example:
Python-Pandas Code:
import numpy as np
import pandas as pd
countries_population = {"Italy": 60550000, "France": 65130728,
"Russia": 435000, "Iceland": 435000,
"Palau": 435000, "Brazil": 21104900,
"Nauru": 11600, "Tuvalu": 11600,
"Bermuda": 11600, "Tokelau": 1440}
s = pd.Series(countries_population)
s
Output:
Italy 60550000 France 65130728 Russia 435000 Iceland 435000 Palau 435000 Brazil 21104900 Nauru 11600 Tuvalu 11600 Bermuda 11600 Tokelau 1440 dtype: int64
Example - The n largest elements where n=5 by default:
Python-Pandas Code:
import numpy as np
import pandas as pd
countries_population = {"Italy": 60550000, "France": 65130728,
"Russia": 435000, "Iceland": 435000,
"Palau": 435000, "Brazil": 21104900,
"Nauru": 11600, "Tuvalu": 11600,
"Bermuda": 11600, "Tokelau": 1440}
s = pd.Series(countries_population)
s.nlargest()
Output:
France 65130728 Italy 60550000 Brazil 21104900 Russia 435000 Iceland 435000 dtype: int64
Example - The n largest elements where n=4. Default keep value is ‘first’ so Russia will be kept:
Python-Pandas Code:
import numpy as np
import pandas as pd
countries_population = {"Italy": 60550000, "France": 65130728,
"Russia": 435000, "Iceland": 435000,
"Palau": 435000, "Brazil": 21104900,
"Nauru": 11600, "Tuvalu": 11600,
"Bermuda": 11600, "Tokelau": 1440}
s = pd.Series(countries_population)
s.nlargest(4)
Output:
France 65130728 Italy 60550000 Brazil 21104900 Russia 435000 dtype: int64
Example - The n largest elements where n=4 and keeping the last duplicates. Palau will be kept since it is the last with value 435000 based on the index order:
Python-Pandas Code:
import numpy as np
import pandas as pd
countries_population = {"Italy": 60550000, "France": 65130728,
"Russia": 435000, "Iceland": 435000,
"Palau": 435000, "Brazil": 21104900,
"Nauru": 11600, "Tuvalu": 11600,
"Bermuda": 11600, "Tokelau": 1440}
s = pd.Series(countries_population)
s.nlargest(4, keep='last')
Output:
France 65130728 Italy 60550000 Brazil 21104900 Palau 435000 dtype: int64
Example - The n largest elements where n=5 with all duplicates kept. Note that the returned Series has five elements due to the three duplicates:
Python-Pandas Code:
import numpy as np
import pandas as pd
countries_population = {"Italy": 60550000, "France": 65130728,
"Russia": 435000, "Iceland": 435000,
"Palau": 435000, "Brazil": 21104900,
"Nauru": 11600, "Tuvalu": 11600,
"Bermuda": 11600, "Tokelau": 1440}
s = pd.Series(countries_population)
s.nlargest(5, keep='all')
Output:
France 65130728 Italy 60550000 Brazil 21104900 Russia 435000 Iceland 435000 Palau 435000 dtype: int64
Previous: Minimum values in Pandas requested axis
Next: Get the smallest n elements in Pandas
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/series/series-nlargest.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics