Pandas Series.unique()

This article explains the usage details of Pandas.Series.unique() in Python with few examples.

In Pandas, the Series class provides a member function unique(), which returns a numpy array of unique elements in the Series.

Series.unique()

Unique values in returned numpy array will be in the order of their appearance in the Series, which means these returned unique values will not be in any sorted order.

Examples of Series.unique()

Let’s understand with an example,

First, we will create a Series object from a list,

Advertisements
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 call the unique() function on this Series object,

# Get a Numpy Array of unique values in Series
unique_values = seres_obj.unique() 

print(unique_values)

Output:

[11 23  4 56 34 55]

It returned a numpy array containing all the unique values from the Series object. Also, the values returned are in the order of their appearance.

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)


# Get a Numpy Array of unique values in Series
unique_values = seres_obj.unique() 

print(unique_values)

Output

0    11
1    23
2     4
3    56
4    34
5    55
6    11
7     4
8    56
9    34
dtype: int64

[11 23  4 56 34 55]

Another example of Pandas.Series.unique()

Let’s see another example, where we will create a Pandas Series of strings and then fetch unique elements from Series using unique() function. For example,

import pandas as pd

# Create Series object from List
names = pd.Series([ 'Ritika',
                    'John',
                    'Ritika',
                    'Shaun',
                    'John',
                    'Ritika',
                    'Mark',
                    'Shaun',
                    ])

print(names)

# Get a Numpy Array of unique values in Series
unique_names = names.unique() 

print(unique_names)

Output:

0    Ritika
1      John
2    Ritika
3     Shaun
4      John
5    Ritika
6      Mark
7     Shaun
dtype: object

['Ritika' 'John' 'Shaun' 'Mark']

Here, it gave us a numpy array of unique strings.

Summary:

Today we learned how to use the unique() function of the Pandas series.

Pandas Tutorials -Learn Data Analysis with Python

   

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.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top