Examples
import numpy as np
import pandas as pd
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"]},
index=["South Africa", "Canada", "Australia",
"Maldives", "Iceland", "Monaco", "Niue",
"Tuvalu", "Palau"])
df
In the following example nlargest() is used to select the three rows having
the largest values in column “population”.
df.nlargest(3, 'population')
When using keep='last', ties are resolved in reverse order:
df.nlargest(3, 'population', keep='last')
When using keep='all', all duplicate items are maintained:
df.nlargest(3, 'population', keep='all')
To order by the largest values in column "population" and then "GDP",
you can specify multiple columns like in the next example:
df.nlargest(3, ['population', 'GDP'])