.unique()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published May 13, 2024
Contribute to Docs

The .unique() function returns unique values from a data series using a hash table. It operates similarly to numpy.unique() but is notably faster, especially with large datasets, and it also includes NA values.

Syntax

pd.unique(data_series)

The data_series parameter represents a 1-dimensional array-like data structure from which unique elements will be returned by the function. The dtype of the return matches that of the input, which can be of Index, Categorical, or Series type. The function lists the unique elements in the order they appear in the input data series, and it does NOT sort them.

Example

The following example demonstrates the use of the .unique() function:

import pandas as pd
series = pd.Series([3, -1, 5, -1, 2, 1, 3, 2, 1, 5, -2, 1, 2])
unique_elements = series.unique()
print(f"The unique elements in series {list(series)} are\n {unique_elements}")

The above code outputs the following:

The unique elements in series [3, -1, 5, -1, 2, 1, 3, -2, 1, 5, 2, 1, 2] are
[3 -1 5 2 1 3 -2]

Codebyte Example

The code below shows off the effects of unique() on different kinds of data types: Index, Categorical, and Series. After defining the array-like objects, the unique() method is applied to list out the unique elements of each object, and the resulting data is printed out to the console.

Code
Output
Loading...

All contributors

Looking to contribute?

Learn Python:Pandas on Codecademy