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 smallest elements where n=5 by default.

In [4]:
s.nsmallest()
Out[4]:
Tokelau      1440
Nauru       11600
Tuvalu      11600
Bermuda     11600
Russia     435000
dtype: int64

The n smallest elements where n=3. Default keep value is ‘first’ so Nauru and Tuvalu will be kept.

In [5]:
s.nsmallest(3)
Out[5]:
Tokelau     1440
Nauru      11600
Tuvalu     11600
dtype: int64

The n smallest elements where n=3 and keeping the last duplicates. Bermuda and Tuvalu will be
kept since they are the last with value 11600 based on the index order.

In [6]:
s.nsmallest(3, keep='last')
Out[6]:
Tokelau     1440
Bermuda    11600
Tuvalu     11600
dtype: int64

The n smallest elements where n=3 with all duplicates kept. Note that the returned Series
has four elements due to the three duplicates.

In [7]:
s.nsmallest(3, keep='all')
Out[7]:
Tokelau     1440
Nauru      11600
Tuvalu     11600
Bermuda    11600
dtype: int64