Python Pandas - Return unique values in the index

To return unique values in a Pandas index, use the index.unique() method. This method returns unique values in their order of first appearance without sorting them.

Syntax

index.unique()

Creating a Pandas Index

First, let's create a Pandas index with duplicate values ?

import pandas as pd

# Creating Pandas index with duplicates
index = pd.Index([10, 50, 70, 10, 90, 50, 10, 30])
print("Original Index:")
print(index)
Original Index:
Int64Index([10, 50, 70, 10, 90, 50, 10, 30], dtype='int64')

Getting Unique Values

Use the unique() method to extract unique values. The values are returned in order of appearance ?

import pandas as pd

index = pd.Index([10, 50, 70, 10, 90, 50, 10, 30])

# Get unique values from the index
unique_values = index.unique()
print("Unique values:")
print(unique_values)
print(f"\nOriginal size: {index.size}")
print(f"Unique values count: {len(unique_values)}")
Unique values:
Int64Index([10, 50, 70, 90, 30], dtype='int64')

Original size: 8
Unique values count: 5

Example with String Index

The unique() method works with different data types ?

import pandas as pd

# String index with duplicates
str_index = pd.Index(['apple', 'banana', 'apple', 'cherry', 'banana', 'date'])
print("String Index:")
print(str_index)

print("\nUnique string values:")
print(str_index.unique())
String Index:
Index(['apple', 'banana', 'apple', 'cherry', 'banana', 'date'], dtype='object')

Unique string values:
Index(['apple', 'banana', 'cherry', 'date'], dtype='object')

Key Points

Feature Description
Order Returns values in order of first appearance
Sorting Does NOT sort the unique values
Data Types Works with numeric, string, and datetime indexes
Return Type Returns the same index type as original

Conclusion

The index.unique() method efficiently extracts unique values from a Pandas index while preserving their order of appearance. Use this method when you need to remove duplicates without sorting the results.

Updated on: 2026-03-26T16:07:40+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements