ในการพิจารณาว่าวัตถุ CategoricalIndex สองรายการมีองค์ประกอบเหมือนกันหรือไม่ ให้ใช้ equals() กระบวนการ. ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
ตั้งค่าหมวดหมู่สำหรับหมวดหมู่โดยใช้พารามิเตอร์ "หมวดหมู่" จัดหมวดหมู่ตามคำสั่งโดยใช้พารามิเตอร์ "สั่งซื้อ" สร้างวัตถุ CategoricalIndex สองรายการ -
catIndex1 = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]) catIndex2 = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
ตรวจสอบวัตถุ CategoricalIndex ทั้งสองเพื่อความเท่าเทียมกัน -
print("\nCheck both the CategoricalIndex objects for equality...\n",catIndex1.equals(catIndex2)) ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import pandas as pd
# Set the categories for the categorical using the "categories" parameter
# Treat the categorical as ordered using the "ordered" parameter
# Create two CategoricalIndex objects
catIndex1 = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
catIndex2 = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
# Display the CategoricalIndex objects
print("CategoricalIndex1...\n",catIndex1)
print("\nCategoricalIndex2...\n",catIndex2)
# Check both the CategoricalIndex objects for equality
print("\nCheck both the CategoricalIndex objects for equality...\n",catIndex1.equals(catIndex2)) ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
CategoricalIndex1... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category') CategoricalIndex2... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category') Check both the CategoricalIndex objects for equality... True