pandas.Index.equals#

Index.equals(other)[源代码]#

确定两个Index对象是否相等。

正在进行比较的事情包括:

  • Index对象内的元素。

  • Index对象中元素的顺序。

参数
other任何

要比较的另一个对象。

退货
布尔尔

如果“Other”是一个索引,并且它具有与调用索引相同的元素和顺序,则为True;否则为False。

示例

>>> idx1 = pd.Index([1, 2, 3])
>>> idx1
Int64Index([1, 2, 3], dtype='int64')
>>> idx1.equals(pd.Index([1, 2, 3]))
True

其中的元素进行了比较

>>> idx2 = pd.Index(["1", "2", "3"])
>>> idx2
Index(['1', '2', '3'], dtype='object')
>>> idx1.equals(idx2)
False

比较顺序

>>> ascending_idx = pd.Index([1, 2, 3])
>>> ascending_idx
Int64Index([1, 2, 3], dtype='int64')
>>> descending_idx = pd.Index([3, 2, 1])
>>> descending_idx
Int64Index([3, 2, 1], dtype='int64')
>>> ascending_idx.equals(descending_idx)
False

数据类型为 not 比较

>>> int64_idx = pd.Int64Index([1, 2, 3])
>>> int64_idx
Int64Index([1, 2, 3], dtype='int64')
>>> uint64_idx = pd.UInt64Index([1, 2, 3])
>>> uint64_idx
UInt64Index([1, 2, 3], dtype='uint64')
>>> int64_idx.equals(uint64_idx)
True