Examples
import numpy as np
import pandas as pd
countries_population = {"Italy": 60550000, "France": 65130728,
"Russia": 435000, "Iceland": 435000,
"Palau": 435000, "Brazil": 21104900,
"Nauru": 11600, "Tuvalu": 11600,
"Bermuda": 11600, "Tokelau": 1440}
s = pd.Series(countries_population)
s
The n smallest elements where n=5 by default.
s.nsmallest()
The n smallest elements where n=3. Default keep value is ‘first’ so Nauru and Tuvalu will be kept.
s.nsmallest(3)
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.
s.nsmallest(3, keep='last')
The n smallest elements where n=3 with all duplicates kept. Note that the returned Series
has four elements due to the three duplicates.
s.nsmallest(3, keep='all')