w3resource

Pandas Series: nsmallest() function

Get the smallest n elements in Pandas

The nsmallest() function is used to get the smallest n elements.

Syntax:

Series.nsmallest(self, n=5, keep='first')
Pandas Series nsmallest image

Parameters:

Name Description Type/Default Value Required / Optional
n Return this many ascending sorted values. int
Default Value: 5
Required
keep When there are duplicate values that cannot all fit in a Series of n elements:
  • first : return the first n occurrences in order of appearance.
  • last : return the last n occurrences in reverse order of appearance.
  • all : keep all occurrences. This can result in a Series of size larger than n.
{‘first’, ‘last’, ‘all’}
Default Value: ‘first’
Required

Returns: The n smallest values in the Series, sorted in increasing order.

Notes: Faster than .sort_values().head(n) for small n relative to the size of the Series object.

Example:

Python-Pandas Code:

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

Output:

Italy      60550000
France     65130728
Russia       435000
Iceland      435000
Palau        435000
Brazil     21104900
Nauru         11600
Tuvalu        11600
Bermuda       11600
Tokelau        1440
dtype: int64
Pandas Series nsmallest image

Example - The n smallest elements where n=5 by default:

Python-Pandas Code:

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.nsmallest()

Output:

Tokelau      1440
Nauru       11600
Tuvalu      11600
Bermuda     11600
Russia     435000
dtype: int64

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

Python-Pandas Code:

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.nsmallest(3)

Output:

Tokelau     1440
Nauru      11600
Tuvalu     11600
dtype: int64

Example - 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:

Python-Pandas Code:

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.nsmallest(3, keep='last')

Output:

Tokelau     1440
Bermuda    11600
Tuvalu     11600
dtype: int64

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

Python-Pandas Code:

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.nsmallest(3, keep='all')

Output:

Tokelau     1440
Nauru      11600
Tuvalu     11600
Bermuda    11600
dtype: int64

Previous: Get the largest n elements in Pandas
Next: Percentage change between the current and a prior element



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/series/series-nsmallest.php