Python Pandas - Determine if two Index objects are equal

To determine if two Index objects are equal in Pandas, use the equals() method. This method performs element-wise comparison and returns True if both Index objects contain the same elements in the same order.

Syntax

Index.equals(other)

Parameters:

  • other − Another Index object to compare with

Returns: boolTrue if both Index objects are equal, False otherwise

Example 1: Equal Index Objects

Let's create two identical Index objects and check if they are equal ?

import pandas as pd

# Creating two identical Index objects
index1 = pd.Index([15, 25, 55, 10, 100, 70, 35, 40, 55])
index2 = pd.Index([15, 25, 55, 10, 100, 70, 35, 40, 55])

# Display both Index objects
print("Index1:")
print(index1)
print("\nIndex2:")
print(index2)

# Check if they are equal
print("\nAre these Index objects equal?")
print(index1.equals(index2))
Index1:
Index([15, 25, 55, 10, 100, 70, 35, 40, 55], dtype='int64')

Index2:
Index([15, 25, 55, 10, 100, 70, 35, 40, 55], dtype='int64')

Are these Index objects equal?
True

Example 2: Different Index Objects

Now let's compare two Index objects with different elements ?

import pandas as pd

# Creating two different Index objects
index1 = pd.Index([15, 25, 55, 10, 100])
index2 = pd.Index([15, 25, 55, 10, 200])  # Last element is different

# Display both Index objects
print("Index1:")
print(index1)
print("\nIndex2:")
print(index2)

# Check if they are equal
print("\nAre these Index objects equal?")
print(index1.equals(index2))
Index1:
Index([15, 25, 55, 10, 100], dtype='int64')

Index2:
Index([15, 25, 55, 10, 200], dtype='int64')

Are these Index objects equal?
False

Example 3: Different Order

The equals() method considers order, so Index objects with same elements in different order are not equal ?

import pandas as pd

# Creating Index objects with same elements but different order
index1 = pd.Index([10, 20, 30])
index2 = pd.Index([30, 20, 10])

print("Index1:")
print(index1)
print("\nIndex2:")
print(index2)

# Check if they are equal
print("\nAre these Index objects equal?")
print(index1.equals(index2))
Index1:
Index([10, 20, 30], dtype='int64')

Index2:
Index([30, 20, 10], dtype='int64')

Are these Index objects equal?
False

Key Points

  • The equals() method performs element-wise comparison
  • Order matters − same elements in different order are considered unequal
  • Both Index objects must have identical length, elements, and order
  • Works with all Index types (string, numeric, datetime, etc.)

Conclusion

Use Index.equals() to compare two Index objects for exact equality. The method returns True only when both Index objects have identical elements in the same order.

---
Updated on: 2026-03-26T16:30:23+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements