ในการพิจารณาว่าวัตถุดัชนีสองวัตถุเท่ากันหรือไม่ ให้ใช้ 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])
แสดงดัชนี1และดัชนี2 -
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