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"]},
index=["South Africa", "Canada", "Australia",
"Maldives", "Iceland", "Monaco", "Niue",
"Tuvalu", "Palau"])
df

Out[2]:
population GDP alpha-2
South Africa 58812491 2033594 SA
Canada 37522972 1833594 CA
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, nsmallest() has used to select the three rows having
the smallest values in column “a”:

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

Out[3]:
population GDP alpha-2
Niue 11646 139 NU
Tuvalu 11646 39 TV
Palau 11646 539 PW

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

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

Out[4]:
population GDP alpha-2
Palau 11646 539 PW
Tuvalu 11646 39 TV
Niue 11646 139 NU

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

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

Out[5]:
population GDP alpha-2
Niue 11646 139 NU
Tuvalu 11646 39 TV
Palau 11646 539 PW

To order by the largest values in column “a” and then “c”, we can specify multiple
columns like in the next example.

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

Out[6]:
population GDP alpha-2
Tuvalu 11646 39 TV
Niue 11646 139 NU
Palau 11646 539 PW