Python Pandas - Determine if two Index objects are equal

PythonPandasServer Side ProgrammingProgramming

To determine if two Index objects are equal, use the equals() method.

At first, import the required libraries −

import pandas as pd

Creating index1 and index2 −

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 the index1 and index2 −

print("Pandas Index1...\n",index1)
print("Pandas Index2...\n",index2)

Check whether two index objects are equal −

index1.equals(index2)

Example

Following is the code −

import pandas as pd

# Creating index1 and index2
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 the index1 and index2
print("Pandas Index1...\n",index1)
print("Pandas Index2...\n",index2)

print("\nAre these two Index objects equal?\n",index1.equals(index2))

Output

This will produce the following code −

Pandas Index1...
Int64Index([15, 25, 55, 10, 100, 70, 35, 40, 55], dtype='int64')
Pandas Index2...
Int64Index([15, 25, 55, 10, 100, 70, 35, 40, 55], dtype='int64')

Are these two Index objects equal?
True
raja
Published on 14-Oct-2021 07:25:49
Advertisements