การเปรียบเทียบ Index สองชุดว่าเหมือนกันทุกด้าน
ใช้เมธอด index1.identical(index2) ตรวจสอบว่า Index ทั้งสองมีค่า ลำดับ dtype และชื่อ (name) เหมือนกันหมด
ขั้นตอน
- นำเข้า pandas
- สร้าง Index สองชุดที่เหมือนกัน
- แสดงทั้งสอง Index
- ใช้
identical()ตรวจสอบ
ตัวอย่างโค้ด
import pandas as pd
# สร้าง Index1 และ Index2 เหมือนกัน
index1 = pd.Index([15, 25, 35, 45, 55])
index2 = pd.Index([15, 25, 35, 45, 55])
# แสดง Index
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
ความแตกต่างจาก equals()
identical() ตรวจสอบ dtype และ name ด้วย ในขณะที่ equals() ตรวจสอบแค่ค่าและลำดับ