.unique()
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 pdseries = 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.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pandas on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Skill path
Data Science Foundations
Learn to clean, analyze, and visualize data with Python and SQL.Includes 15 CoursesWith CertificateBeginner Friendly54 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours