Built with Tutify.io

    AIStudyLabs

    Check Values of Pandas Series is Unique

    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.

    Key Points –

    <a id="h-quick-examples-of-check-every-series-value-is-unique"></a>

    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]))
    

    <a id="h-syntax-of-pandas-series-is-unique"></a>

    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.

    <a id="h-check-pandas-series-contains-unique-values"></a>

    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
    

    <a id="h-use-is-unique-attribute-with-nan-values-to-check-unique-values"></a>

    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
    

    <a id="h-use-nunique-function-to-check-series-unique-value"></a>

    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
    

    <a id="h-for-performance-is-unique-is-optimized-and-faster-than-manual-checks-with-nunique-faq-on-check-values-of-pandas-series-is-unique"></a>

    For performance, is_unique is optimized and faster than manual checks with nunique().

    FAQ on Check Values of Pandas Series is 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.

    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.

    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.

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

    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.

    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