Pandas Series.nunique()

This article explains the usage details of Pandas.Series.nunique() in Python with a few examples.

In Pandas, the Series class provides a member function nunique(), which returns a count of unique elements.

pandas.Series.nunique()

Series.nunique(dropna=True)
  • Returns:
    • The number of unique values in the Series.

By default, it excludes the NaN while counting unique values. If you want to include NaN, then pass the dropna argument with value False.

Examples of Series.nunique() function

First, we will create a Series object from a list,

import pandas as pd
import numpy as np

# Create Series object from List
seres_obj = pd.Series([11, 23, 4, 56, np.NaN, 34, 55, 11, 4, 56, 34])

print(seres_obj)

Output:

0     11.0
1     23.0
2      4.0
3     56.0
4      NaN
5     34.0
6     55.0
7     11.0
8      4.0
9     56.0
10    34.0
dtype: float64

Our Series object contains many duplicate elements. Now let’s call the nunique() function on this Series object,

# Get Count of Unique elements in Series
count = seres_obj.nunique() 

print('Count of Unique values: ', count)

Output:

Count of Unique values:  6

It returned a count of the unique values from the Series object. By default, it excluded the NaN from the calculation. Let’s see another example where we will include NaN values too.

Examples of Series.nunique() with dropna

import pandas as pd
import numpy as np

# Create Series object from List
seres_obj = pd.Series([11, 23, 4, 56, np.NaN, 34, 55, 11, 4, 56, 34])

print(seres_obj)

# Get Count of Unique elements in Series including NaN
count = seres_obj.nunique(dropna=False) 

print('Count of Unique values: ', count)

Output:

0     11.0
1     23.0
2      4.0
3     56.0
4      NaN
5     34.0
6     55.0
7     11.0
8      4.0
9     56.0
10    34.0
dtype: float64

Count of Unique values:  7

As we passed the dropna argument with value False to the nunique() function. Therefore, it returned the count of unique values in Series, including NaN.

Another example of Pandas.Series.nunique()

Let’s see another example, where we will create a Pandas Series of strings and then fetch the count of unique elements from Series using nunique() function. For example,

import pandas as pd

# Create Series object from List
names = pd.Series([ 'Ritika',
                    'John',
                    'Ritika',
                    'Shaun',
                    'John',
                    'Ritika',
                    'Mark',
                    'Shaun',
                    ])

print(names)

# Get Count of Unique elements in Series
count = names.nunique() 

print('Count of Unique Names: ', count)

Output:

0    Ritika
1      John
2    Ritika
3     Shaun
4      John
5    Ritika
6      Mark
7     Shaun
dtype: object

Count of Unique Names:  4

Summary:

Today we learned how to use the nunique() function of the Pandas series.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top