ในการพิจารณาว่าวัตถุดัชนีสองรายการที่มีลำดับตรงข้ามเท่ากันหรือไม่ ให้ใช้ equals() วิธีการ
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
การสร้างดัชนี Pandas1 และ index2 -
index1 = pd.Index([15, 25, 35, 45, 55, 65, 75, 85, 95]) index2 = pd.Index([95, 85, 75, 65, 55, 45, 35, 25, 15])
แสดงดัชนี1และดัชนี2 -
print("Pandas Index1...\n",index1)
print("Pandas Index2...\n",index2) ตรวจสอบว่าวัตถุดัชนีสองวัตถุที่มีลำดับตรงข้ามเท่ากันหรือไม่ -
print("\nAre two Index objects with opposite order equal?"
"\n",index1.equals(index2)) ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import pandas as pd
# Creating Pandas index1 and index2
index1 = pd.Index([15, 25, 35, 45, 55, 65, 75, 85, 95])
index2 = pd.Index([95, 85, 75, 65, 55, 45, 35, 25, 15])
# Display the index1 and index2
print("Pandas Index1...\n",index1)
print("Pandas Index2...\n",index2)
print("\nAre two Index objects with opposite order equal?"
"\n",index1.equals(index2)) ผลลัพธ์
สิ่งนี้จะสร้างรหัสต่อไปนี้ -
Pandas Index1... Int64Index([15, 25, 35, 45, 55, 65, 75, 85, 95], dtype='int64') Pandas Index2... Int64Index([95, 85, 75, 65, 55, 45, 35, 25, 15], dtype='int64') Are two Index objects with opposite order equal? False