Examples

In [1]:
import numpy as np
import pandas as pd

In [2]:
df = pd.DataFrame({'population': [58812491, 37522972, 339757,
339757, 339757, 38964, 11646, 11646, 11646],
'GDP': [2033594, 1833594 , 153594, 14520, 10128,
836, 139, 39, 539],
'alpha-2': ["SA", "CA", "AU", "MV", "IS",
"MC", "NU", "TV", "PW"]},
"Maldives", "Iceland", "Monaco", "Niue",
"Tuvalu", "Palau"])
df

Out[2]:
population GDP alpha-2
South Africa 58812491 2033594 SA
Australia 339757 153594 AU
Maldives 339757 14520 MV
Iceland 339757 10128 IS
Monaco 38964 836 MC
Niue 11646 139 NU
Tuvalu 11646 39 TV
Palau 11646 539 PW

In the following example nlargest() is used to select the three rows having
the largest values in column “population”.

In [3]:
df.nlargest(3, 'population')

Out[3]:
population GDP alpha-2
South Africa 58812491 2033594 SA
Australia 339757 153594 AU

When using keep='last', ties are resolved in reverse order:

In [4]:
df.nlargest(3, 'population', keep='last')

Out[4]:
population GDP alpha-2
South Africa 58812491 2033594 SA
Iceland 339757 10128 IS

When using keep='all', all duplicate items are maintained:

In [5]:
df.nlargest(3, 'population', keep='all')

Out[5]:
population GDP alpha-2
South Africa 58812491 2033594 SA
Australia 339757 153594 AU
Maldives 339757 14520 MV
Iceland 339757 10128 IS

To order by the largest values in column "population" and then "GDP",
you can specify multiple columns like in the next example:

In [6]:
df.nlargest(3, ['population', 'GDP'])

Out[6]:
population GDP alpha-2
South Africa 58812491 2033594 SA