How to check each value of a pandas series is unique or not?n



The pandas.Series constructor have an attribute called is_unique. which is used to check whether the data present in the pandas series object is unique or not. As we know, the pandas series object is a single-dimensional data structure, which stores any type of data with label representation.

By using the “is_unque” attribute we can check if all data present in the series object holds unique values or not. And it returns a boolean value as an output.

It returns “True” if the data present in the given series object is unique, otherwise, it will return “False”.

Example 1

import pandas as pd

# creating pandas Series with date sequence
series = pd.Series(['2021-01-01','2021-01-02','2021-01-02', '2021-01-03','2021-01-05'])

print(series)

# apply is_unique property
print("Is Unique: ", series.is_unique)

Explanation

Here, we have initialized a Series with a list of data sequences of length 5. Then we applied the is_unique property to verify if the data present in the given series object is unique or not.

Output

0 2021-01-01
1 2021-01-02
2 2021-01-02
3 2021-01-03
4 2021-01-05
dtype: object

Is Unique: False

In the above output block, we can see the given series object, and also the boolean value “False”. The output boolean value “False” is the returned from the canis_unique attribute, which means that the data present in the given series is having duplicate data.

Example 2

import pandas as pd

# creating pandas Series with list of integers
series = pd.Series([1,2,3,4,5,6])

print(series)

# apply is_unique property
print("Is Unique: ", series.is_unique)

Explanation

Let’s take another example for checking if the values in a series object is unique or not. Here, we have created a series object with a python list of integers. And that applied the is_unique attribute to the given pandas series object.

Output

0 1
1 2
2 3
3 4
4 5
5 6
dtype: int64

Is Unique: True

We can see both the series object and also the output from the is_unique attribute is displayed in the above output block. We got the boolean value “True” as a result of the is_anique attribute, so we can say that the data in the given series object is having all unique values.


Advertisements