Python Pandas - Check if the index has duplicate values

To check if the index has duplicate values, use the has_duplicates property in Pandas. This property returns True if any values appear more than once in the index, and False otherwise.

Syntax

index.has_duplicates

Creating an Index with Duplicates

Let's create an index with duplicate values and check for duplicates ?

import pandas as pd

# Creating the index with duplicates
index = pd.Index(['Car', 'Bike', 'Truck', 'Car', 'Airplane'])

# Display the index
print("Pandas Index...")
print(index)

# Check if the index has duplicate values
print("\nHas duplicate values?")
print(index.has_duplicates)
Pandas Index...
Index(['Car', 'Bike', 'Truck', 'Car', 'Airplane'], dtype='object')

Has duplicate values?
True

Creating an Index without Duplicates

Now let's create an index without duplicates to see the difference ?

import pandas as pd

# Creating the index without duplicates
index_unique = pd.Index(['Car', 'Bike', 'Truck', 'Bus', 'Airplane'])

# Display the index
print("Pandas Index...")
print(index_unique)

# Check if the index has duplicate values
print("\nHas duplicate values?")
print(index_unique.has_duplicates)
Pandas Index...
Index(['Car', 'Bike', 'Truck', 'Bus', 'Airplane'], dtype='object')

Has duplicate values?
True

Using with Different Data Types

The has_duplicates property works with various data types including numeric indices ?

import pandas as pd

# Numeric index with duplicates
numeric_index = pd.Index([1, 2, 3, 2, 5])
print("Numeric index:", numeric_index.has_duplicates)

# DateTime index with duplicates
date_index = pd.date_range('2024-01-01', periods=3).append(pd.date_range('2024-01-01', periods=1))
print("DateTime index:", date_index.has_duplicates)
Numeric index: True
DateTime index: True

Conclusion

The has_duplicates property provides a quick way to check if an index contains duplicate values. It returns True for duplicates and False for unique indices, working with all data types.

Updated on: 2026-03-26T16:13:17+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements