Pandas Series.is_unique

This article explains the usage details of Pandas.Series.is_unique in Python with few examples.

In Pandas, the Series class provides a member variable is_unique, whose value will return True if all Series elements are unique.

pandas.Series.is_unique

It is True if all elements in the Series are unique and False if the Series contains any duplicate value.

Examples of Series.is_unique

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

import pandas as pd

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

print(seres_obj)

Output:

0    11
1    23
2     4
3    56
4    34
5    55
6    11
7     4
8    56
9    34
dtype: int64

Our Series object contains many duplicate elements. Now let’s use Series.is_unique to check if Series has any duplicates or all unique values.

# Check if all values in Series are unique
if seres_obj.is_unique:
    print('Yes, All values in Series are unique')
else:
    print('No, There are duplicates in the Series')

Output:

No, There are duplicates in the Series

As values in our Series are not unique, therefore it printed that the Series contains duplicates.

The complete example is as follows,

import pandas as pd

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

print(seres_obj)


# Check if all values in Series are unique
if seres_obj.is_unique:
    print('Yes, All values in Series are unique')
else:
    print('No, There are duplicates in the Series')

Output

0    11
1    23
2     4
3    56
4    34
5    55
6    11
7     4
8    56
9    34
dtype: int64

No, There are duplicates in the Series

Another example of Pandas.Series.is_unique

Let’s see another example, where we will create a Pandas Series of strings and then check if the Series contains all unique elements or not. For example,

import pandas as pd

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


print(names)


# Check if all values in Series are unique
if names.is_unique:
    print('Yes, All values in Series are unique')
else:
    print('No, There are duplicates in the Series')

Output:

0    Ritika
1      John
2      Mark
3     Shaun
4    Joseph
5    Pulkit
6      Lisa
7     Peter
dtype: object

Yes, All values in Series are unique

As there are no duplicates in our Series, it printed that all elements in the Series are unique.

Summary:

Today we learned how to use the is_unique function of the Pandas series.

Advertisements

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