• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:15 mins read
You are currently viewing Check Values of Pandas Series is Unique

Pandas Series.is_unique attribute is used to check every element or value present in the pandas series object holds unique values or not. It returns True if the elements present in the given series object is unique, otherwise, it returns False. In this article, I will explain Series.is_unique and use this function how to check if pandas series contains all unique values or not.

Advertisements

Key Points –

  • is_unique attribute in Pandas Series quickly determines if all values in the Series are unique or not.
  • It offers a simple and efficient approach for assessing the uniqueness of data within a Series, aiding in data quality assurance tasks.
  • It can be useful for identifying if a Series contains duplicate values or not.
  • This attribute can help in data validation and cleaning processes.
  • The is_unique attribute operates efficiently even on large datasets.

Quick Examples of Check Every Series Value is Unique

If you hurry below are quick examples of how to check every value in Pandas series is unique or not.


# Quick examples of check every series value is unique

# Example 1: Usage of Series.is_unique
ser2 = ser.is_unique

# Example 2: Check the pandas series unique or not
ser = pd.Series(['Spark','PySpark','Pandas','NumPy']).is_unique

# Example 3: Use pandas.series() function 
# To apply is_unique property
ser = pd.Series(['Spark','PySpark','Pandas','PySpark']).is_unique

# Example 4: Use is_unique attribute with nan values 
# To check unique values
ser = pd.Series(['Spark','PySpark','Pandas','NumPy',np.nan,np.nan]).is_unique

# Example 5: Use dropna() & is_unique attribute 
# With nan values to check unique values
ser = pd.Series(['Spark','PySpark','Pandas', np.nan, np.nan]).dropna().is_unique

# Example 6: Use nunique() function 
# To check series unique value
ser = pd.Series([1, 2, 3]).nunique()==len(pd.Series([1, 2, 3]))

Syntax of Pandas Series.is_unique

Following is the syntax of Series.is_unique attribute.


# Syntax of Series.is_unique
Series.is_unique

It returns a boolean value indicating whether all values in the series are unique or not.

Check Pandas Series Contains Unique Values

Use the pandas Series.is_unique attribute to check whether every data or element in the given Series object is a unique value or not. If this attribute returns True it will indicate the given series object is unique.

Pandas Series is a one-dimensional, Index-labeled data structure available only in the Pandas library. It can store all the datatypes such as strings, integers, float, and other python objects. We can access each element in the Series with the help of corresponding default indices. Now, let’s create pandas series using a list of values.


import pandas as pd
import numpy as np
  
# Create the Series
ser = pd.Series(['Spark','PySpark','Pandas','NumPy'])
print(ser)

Yields below output.


# Output:
0      Spark
1    PySpark
2     Pandas
3      NumPy
dtype: object

Now, let’s use the Series.is_unique attribute to check if it contains unique values.


# Usage Series.is_unique 
ser2 = ser.is_unique
print(ser2)

# Output:
# True

Check Pandas Series Unique or Not

Created a pandas series using a Python list of strings, then apply is_unique attribute to the given series object. It will return the boolean value(either True or False). Let’s check whether the data given series object is unique or not.


# Check the pandas series unique or not
ser = pd.Series(['Spark','PySpark','Pandas','NumPy']).is_unique
print(ser)

# Output:
# True

# Use pandas.series() function to apply is_unique property
ser = pd.Series(['Spark','PySpark','Pandas','PySpark']).is_unique
print(ser)

# Output
# False

Use is_unique Attribute with NaN Values to Check Unique Values

Created a pandas series object with multiple NaN values, then call is_unique attribute, it returns the boolean value False,

When using the is_unique attribute with a Pandas Series containing NaN values, it will still correctly identify the uniqueness of non-NaN values. However, NaN values are considered as unique.


# Use is_unique attribute with nan values to check unique values
ser = pd.Series(['Spark','PySpark','Pandas','NumPy',np.nan,np.nan]).is_unique
print(ser)

# Output:
# False

To ignore the NaN values, first call dropna() function to drop all NaN values and then call the is_unique.


# Use dropna() & is_unique attribute with nan values to check unique values
ser = pd.Series(['Spark','PySpark','Pandas', np.nan, np.nan]).dropna().is_unique
print(ser)

# Output:
# True

Use nunique() Function to Check Series Unique Value

Alternatively, we can use nunique() function to check the values of the series objects are unique or not.


# Use nunique() function to check series unique value
ser = pd.Series([1, 2, 3]).nunique()==len(pd.Series([1, 2, 3]))
print(ser)

# Output:
# True

Frequently Asked Questions on

What does it mean for a Pandas Series to be unique?

In the context of a Pandas Series, uniqueness refers to the absence of duplicate values within the series. A Pandas Series is considered unique if each element in the series appears only once. If there are no duplicate values, the series is said to be unique. Conversely, if there are repeated values within the series, it is not considered unique.

How can I check if the values in a Pandas Series are unique?

You can use the is_unique attribute of a Pandas Series. It returns a boolean value indicating whether all values in the Series are unique.

Does the is_unique attribute consider NaN values?

By default, NaN values are considered unique. If you want to ignore NaN values, you can first call the dropna() function to remove them and then use the is_unique attribute.

Can I count the number of unique values in a Pandas Series?

You can use the nunique() function. It returns the number of unique values in the Series.

How can I efficiently check if all values in a Pandas Series are unique?

One way is to compare the number of unique values (nunique()) with the total number of elements in the Series (len(series)). If they are equal, then all values are unique.

Is checking for uniqueness important in data analysis tasks?

Checking for uniqueness helps ensure data quality and can uncover potential issues such as duplicate entries or data inconsistencies. It’s a crucial step in data preprocessing and validation.

Conclusion

In this article, I have explained how to check every series value is unique by using pandas Series.is_unique attribute.

Happy Learning !!

Related Articles

References