Pandas Series Series.nunique() Function

  1. Syntax of pandas.Series.nunique():
  2. Example Codes: Series.nunique() Method
  3. Example Codes: Series.nunique() Method With dropna=False

Python Pandas Series.nunique() method counts the unique values in the Python Pandas Series.

Syntax of pandas.Series.nunique():

Series.nunique(dropna=True)

Parameters

dropna True by default.
If True, NaN is excluded. If False, NaN is also counted.

Return

It returns an integer that counts the unique values in the caller Pandas Series.

Example Codes: Series.nunique() Method

import pandas as pd
import numpy as np

ser = pd.Series([1, 2, 3, np.nan, 3, 4, np.nan],
               name = 'No.')

print(ser.nunique())

Output:

4

The caller Series has 4 unique values - [1, 2, 3, 4] except NaN; therefore, Series.nunique() method returns 4 because NaN is excluded by default.

Example Codes: Series.nunique() Method With dropna=False

import pandas as pd
import numpy as np

ser = pd.Series([1, 2, 3, np.nan, 3, 4, np.nan],
               name = 'No.')

print(ser.nunique(dropna=False))

Output:

5

When dropna is False, NaN is also counted in Series.nunique() method.

Related Article - Pandas Series

  • Pandas Series Series.unique() Function
  • Pandas Series Series.value_counts() Function
  • comments powered by Disqus