Python Pandas - 检查两个 Index 对象是否具有相似的对象属性和类型
要检查两个Index对象是否具有相似的对象属性和类型,请使用index1.identical(index2)方法。
首先,导入所需的库——
import pandas as pd
创建Pandasindex1和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("\nThe two Index objects have similar attributes and types?" "\n",index1.identical(index2))
示例
以下是代码-
import pandas as pd #创建Pandasindex1和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("\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