- API reference
- Series
- pandas.Serie...
pandas.Series.nlargest#
- Series.nlargest(n=5,keep='first')[source]#
Return the largestn elements.
- Parameters:
- nint, default 5
Return this many descending sorted values.
- keep{‘first’, ‘last’, ‘all’}, default ‘first’
When there are duplicate values that cannot all fit in aSeries ofn elements:
first
: return the firstn occurrences in orderof appearance.last
: return the lastn occurrences in reverseorder of appearance.all
: keep all occurrences. This can result in a Series ofsize larger thann.
- Returns:
- Series
Then largest values in the Series, sorted in decreasing order.
See also
Series.nsmallest
Get then smallest elements.
Series.sort_values
Sort Series by values.
Series.head
Return the firstn rows.
Notes
Faster than
.sort_values(ascending=False).head(n)
for smallnrelative to the size of theSeries
object.Examples
>>>countries_population={"Italy":59000000,"France":65000000,..."Malta":434000,"Maldives":434000,..."Brunei":434000,"Iceland":337000,..."Nauru":11300,"Tuvalu":11300,..."Anguilla":11300,"Montserrat":5200}>>>s=pd.Series(countries_population)>>>sItaly 59000000France 65000000Malta 434000Maldives 434000Brunei 434000Iceland 337000Nauru 11300Tuvalu 11300Anguilla 11300Montserrat 5200dtype: int64
Then largest elements where
n=5
by default.>>>s.nlargest()France 65000000Italy 59000000Malta 434000Maldives 434000Brunei 434000dtype: int64
Then largest elements where
n=3
. Defaultkeep value is ‘first’so Malta will be kept.>>>s.nlargest(3)France 65000000Italy 59000000Malta 434000dtype: int64
Then largest elements where
n=3
and keeping the last duplicates.Brunei will be kept since it is the last with value 434000 based onthe index order.>>>s.nlargest(3,keep='last')France 65000000Italy 59000000Brunei 434000dtype: int64
Then largest elements where
n=3
with all duplicates kept. Notethat the returned Series has five elements due to the three duplicates.>>>s.nlargest(3,keep='all')France 65000000Italy 59000000Malta 434000Maldives 434000Brunei 434000dtype: int64