Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
countries_population = {"Italy": 60550000, "France": 65130728,
                        "Russia": 435000, "Iceland": 435000,
                        "Palau": 435000, "Brazil": 21104900,
                        "Nauru": 11600, "Tuvalu": 11600,
                        "Bermuda": 11600, "Tokelau": 1440}
In [3]:
s = pd.Series(countries_population)
s
Out[3]:
Italy      60550000
France     65130728
Russia       435000
Iceland      435000
Palau        435000
Brazil     21104900
Nauru         11600
Tuvalu        11600
Bermuda       11600
Tokelau        1440
dtype: int64

The n largest elements where n=5 by default.

In [4]:
s.nlargest()
Out[4]:
France     65130728
Italy      60550000
Brazil     21104900
Russia       435000
Iceland      435000
dtype: int64

The n largest elements where n=4. Default keep value is ‘first’ so Russia will be kept.

In [5]:
s.nlargest(4)
Out[5]:
France    65130728
Italy     60550000
Brazil    21104900
Russia      435000
dtype: int64

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.

In [6]:
s.nlargest(4, keep='last')
Out[6]:
France    65130728
Italy     60550000
Brazil    21104900
Palau       435000
dtype: int64

The n largest elements where n=5 with all duplicates kept. Note that the returned Series has five elements
due to the three duplicates.

In [7]:
s.nlargest(5, keep='all')
Out[7]:
France     65130728
Italy      60550000
Brazil     21104900
Russia       435000
Iceland      435000
Palau        435000
dtype: int64