Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
Pandas unique()
In this tutorial, we will learn about the unique() method in Pandas with the help of examples.
The unique() method in Pandas is used to obtain unique elements of a Series.
Example
import pandas as pd
# create a Series
data = pd.Series([1, 2, 3, 2, 4, 3, 5, 6, 5])
# use unique() to get unique elements
unique_values = data.unique()
print(unique_values)
# Output: [1 2 3 4 5 6]
unique() Syntax
The syntax of the unique() method in Pandas is:
Series.unique()
unique() Return Value
The unique() method returns an array containing the unique values found in the Series.
Example1: Get Unique Elements of an Integer Series
import pandas as pd
# create a Series
int_series = pd.Series([10, 20, 10, 30, 20, 40])
# use unique() to get unique elements
unique_values = int_series.unique()
print(unique_values)
Output
[10 20 30 40]
In the above example, we have applied the unique() method to int_series to find all unique elements.
Example 2: Get Unique Elements of an String Series
import pandas as pd
# create a Series
series_data = pd.Series(['apple', 'banana', 'apple', 'orange', 'banana', 'kiwi'])
# apply the unique() method
unique_fruits = series_data.unique()
print(unique_fruits)
Output
['apple' 'banana' 'orange' 'kiwi']
Here, upon applying the unique() method, the output is an array containing the unique fruit names: ['apple', 'banana', 'orange', 'kiwi'].