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

pythonserver side programmingprogramming更新于 2025/12/28 7:08:17

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

首先,导入所需的库 -

import pandas as pd

创建 Pandas index1 和 index2 −

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

显示 index1 和 index2 −

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

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

print("\n两个 Index 对象具有相似的属性和类型?" "\n",index1.identical(index2))

示例

以下是代码 −

import pandas as pd

# 创建 Pandas index1 和 index2
index1 = pd.Index([15, 25, 35, 45, 55])
index2 = pd.Index([15, 25, 35, 45, 55])

# 显示 index1 和 index2
print("Pandas Index1...\n",index1)
print("Pandas Index2...\n",index2)

print("\n这两个 Index 对象具有相似的属性和类型?" "\n",index1.identical(index2))

输出

将产生以下输出 −

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

这两个 Index 对象具有相似的属性和类型?
True

相关文章


有用资源