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.
1. 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.
# Below are quick examples
# 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]))
2. 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.
3. 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 ti contains unique values.
# Usage Series.is_unique
ser2 = ser.is_unique
print(ser2)
# Output:
# True
5. 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
6. 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
,
# 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
7. 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
8. 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
- Apply Multiple Filters to Pandas DataFrame or Series
- How to Convert NumPy Array to Pandas Series
- Convert Series to Dictionary(Dict) in Pandas
- How to Merge Series into Pandas DataFrame
- How to Get Index of Series in Pandas
- Pandas Remove Elements From Series
- Pandas Series filter() Function
- How to Sort pandas Series
- How to replace Pandas Series?
- How to append Pandas Series?
- How to rename Pandas Series?
- How to get floor or ceil values of Pandas Series?
- How to reshape the Pandas Series?
- How to get Values from Pandas Series?