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, nsmallest() has used to select the three rows having
the smallest values in column “a”:
df.nsmallest(3, 'population')
When using keep='last', ties are resolved in reverse order:
df.nsmallest(3, 'population', keep='last')
When using keep='all', all duplicate items are maintained:
df.nsmallest(3, 'population', keep='all')
To order by the largest values in column “a” and then “c”, we can specify multiple
columns like in the next example.
df.nsmallest(3, ['population', 'GDP'])