Pandas Series unique() Method

Pandas Series unique() method is used to find and return the unique values in the order they appear, effectively filtering out duplicates.

This method is beneficial for categorical data. It preserves the order of the unique elements as they appear in the original Series.

It can be used on Series with any data type (e.g., numeric, string).

One thing to note is that NaN values are also considered unique values.

Syntax

Series.unique()

Parameters

This method does not take any arguments.

Return Value

It returns a NumPy array of unique values in the Series.

Example 1: Basic usage

Basic understanding of Pandas Series unique() Method

import pandas as pd

srs = pd.Series([1, 2, 2, 3, 4, 4, 5])

print(srs.unique())

Output

[1 2 3 4 5]

This method returns the unique values from the given series.

Example 2: Usage with string values

Visual representation of usage with string values

import pandas as pd

srs = pd.Series(['apple', 'banana', 'apple', 'cherry', 'cherry'])

print(srs.unique())

Output

['apple' 'banana' 'cherry']

We get the unique string values in the series.

Example 3: Usage with NaN values

Usage with NaN values

import pandas as pd
import numpy as np

srs = pd.Series([1, 2, np.nan, 4, np.nan])

print(srs.unique())

Output

[ 1. 2. nan 4.]

Example 4: Unique values in a Date Series

Unique values in a Date Series

import pandas as pd

srs = pd.Series(pd.to_datetime(['2021-01-01', '2021-01-02', '2021-01-01']))

print(srs.unique())

Output

<DatetimeArray>
  [ '2021-01-01 00:00:00', 
    '2021-01-02 00:00:00'
  ]

Length: 2, dtype: datetime64[ns]

In this code, we have shown unique dates in a series.

Example 5: Unique Values in a categorical series

import pandas as pd

srs = pd.Series(pd.Categorical(["test", "train", "test", "evaluate"]))

print(srs.unique())

Output

['test', 'train', 'evaluate']

Categories (3, object): ['evaluate', 'test', 'train']

In our code, this method returns the unique categories in a categorical series.

Leave a Comment

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