Python Pandas - Check whether the two Index objects have similar object attributes and types

PythonServer Side ProgrammingProgramming

To check whether the two Index objects have similar object attributes and types, use the index1.identical(index2) method.

At first, import the required libraries -

import pandas as pd

Creating Pandas index1 and index2 −

index1 = pd.Index([15, 25, 35, 45, 55])
index2 = pd.Index([15, 25, 35, 45, 55])

Display the index1 and index2 −

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

Check whether the two index objects have similar attributes and types or not −

print("\nThe two Index objects have similar attributes and types?" "\n",index1.identical(index2))

Example

Following is the code −

import pandas as pd

# Creating Pandas index1 and index2
index1 = pd.Index([15, 25, 35, 45, 55])
index2 = pd.Index([15, 25, 35, 45, 55])

# Display the index1 and index2
print("Pandas Index1...\n",index1)
print("Pandas Index2...\n",index2)

print("\nThe two Index objects have similar attributes and types?" "\n",index1.identical(index2))

Output

This will produce the following output −

Pandas Index1...
Int64Index([15, 25, 35, 45, 55], dtype='int64')
Pandas Index2...
Int64Index([15, 25, 35, 45, 55], dtype='int64')

The two Index objects have similar attributes and types?
True
raja
Published on 13-Oct-2021 06:59:57

Advertisements