Python Pandas - 檢查兩個索引物件是否有相似的物件屬性和型別
要檢查兩個索引物件是否具有相似的物件屬性和型別,請使用 index1.identical(index2) 方法。
首先,匯入所需的庫 -
import pandas as pd
建立 Pandas 索引 1 和索引 2 −
index1 = pd.Index([15, 25, 35, 45, 55]) index2 = pd.Index([15, 25, 35, 45, 55])
顯示索引 1 和索引 2 -
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 # Creating Pandas index1 and index2 index1 = pd.Index([15, 25, 35, 45, 55]) index2 = pd.Index([15, 25, 35, 45, 55]) # Display the index1 and 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
廣告