Python Pandas - 確定兩個索引物件是否相等


若要確定兩個索引物件是否相等,請使用 equals() 方法。

首先,匯入所需的庫 −

import pandas as pd

建立 index1 和 index2 −

index1 = pd.Index([15, 25, 55, 10, 100, 70, 35, 40, 55])
index2 = pd.Index([15, 25, 55, 10, 100, 70, 35, 40, 55])

顯示 index1 和 index2 −

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

檢查兩個索引物件是否相等 −

index1.equals(index2)

示例

以下是程式碼 −

import pandas as pd

# Creating index1 and index2
index1 = pd.Index([15, 25, 55, 10, 100, 70, 35, 40, 55])
index2 = pd.Index([15, 25, 55, 10, 100, 70, 35, 40, 55])

# Display the index1 and index2
print("Pandas Index1...\n",index1)
print("Pandas Index2...\n",index2)

print("\nAre these two Index objects equal?\n",index1.equals(index2))

輸出

將生成以下程式碼 −

Pandas Index1...
Int64Index([15, 25, 55, 10, 100, 70, 35, 40, 55], dtype='int64')
Pandas Index2...
Int64Index([15, 25, 55, 10, 100, 70, 35, 40, 55], dtype='int64')

Are these two Index objects equal?
True

更新於:14-Oct-2021

911 次瀏覽

助你事業騰飛

學完本課程獲得認證

開始
廣告
© . All rights reserved.