📜  Python|熊猫索引.equals()

📅  最后修改于: 2022-05-13 01:55:24.435000             🧑  作者: Mango

Python|熊猫索引.equals()

Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。

Pandas Index.equals()函数确定两个 Index 对象是否包含相同的元素。如果它们包含相同的元素,则该函数返回True ,否则该函数返回False ,表示两个索引中包含的值不同。

示例 #1:使用Index.equals()函数检查两个索引是否包含相同的元素

# importing pandas as pd
import pandas as pd
  
# Creating the first Index
idx1 = pd.Index(['Labrador', 'Beagle', 'Labrador', 'Lhasa', 'Husky', 'Beagle'])
  
# Creating the second Index
idx2 = pd.Index(['Labrador', 'Beagle', 'Pug', 'Lhasa', 'Husky', 'Pitbull'])
  
# Print the first and second Index
print(idx1, "\n", idx2)

输出 :

让我们检查两个索引是否相等。

# Checking the equality of the two Indexes
idx1.equals(idx2)

输出 :

正如我们在输出中看到的那样, Index.equals()函数返回False ,表示索引不相等。示例 #2:使用Index.equals()函数检查两个索引的相等性。

# importing pandas as pd
import pandas as pd
  
# Creating the first Index
idx1 = pd.Index(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
  
# Creating the second Index
idx2 = pd.Index(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
  
# Print the first and second Index
print(idx1, "\n", idx2)

输出 :

让我们检查两个索引是否彼此相等。

# test the equality
idx1.equals(idx2)

输出 :

该函数返回True表示两个索引彼此相等。