Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ตรวจสอบว่า CategoricalIndex มีลำดับหรือไม่ ด้วยคุณสมบัติ ordered

คุณสมบัติ ordered ของ CategoricalIndex คืนค่า True หากหมวดหมู่มีความหมายทางลำดับ (ordered) หรือ False หากไม่มีลำดับ

ตัวอย่างโค้ด

import pandas as pd

catIndex = pd.CategoricalIndex(
    ["p", "q", "r", "s", "p", "q", "r", "s"],
    ordered=True,
    categories=["p", "q", "r", "s"]
)

print("CategoricalIndex:\n", catIndex)
print("\nหมวดหมู่:\n", catIndex.categories)
print("\nมีลำดับหรือไม่ (ordered):\n", catIndex.ordered)

ผลลัพธ์

CategoricalIndex:
CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category')

หมวดหมู่:
Index(['p', 'q', 'r', 's'], dtype='object')

มีลำดับหรือไม่ (ordered):
True

คุณสมบัติ ordered ใช้ตรวจสอบสถานะลำดับได้อย่างรวดเร็ว โดยไม่ต้องเรียกเมธอด