Python Pandas - 检查两个索引对象是否有相似的对象属性和类型


要检查两个索引对象是否具有相似的对象属性和类型,请使用 index1.identical(index2) 方法。

首先,导入所需的库 -

import pandas as pd

创建 Pandas 索引 1 和索引 2 −

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

显示索引 1 和索引 2 -

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

检查两个索引对象是否具有相似属性和类型 -

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

示例

以下是代码 -

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))

输出

这将产生以下输出 -

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

更新于: 2021-10-13

82 浏览

开启你的职业生涯

通过完成课程获得认证

开始
广告