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.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.