Python Pandas CategoricalIndex - 檢查類別是否具有有序關係
若要檢視類別是否具有有序關係,請使用 CategoricalIndex 的ordered 屬性。
首先,匯入所需庫 −
import pandas as pd
使用 “categories”引數為類別設定類別。使用 “ordered”引數將類別視為有序 −
catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
顯示分類索引 −
print("Categorical Index...\n",catIndex)
獲取類別 −
print("\nDisplayingCategories from CategoricalIndex...\n",catIndex.categories)檢查類別是否有序關係 −
print("\nDoes categories have ordered relationship...\n",catIndex.ordered)
示例
以下是程式碼 −
import pandas as pd
# CategoricalIndex can only take on a limited, and usually fixed, number of possible values
# Set the categories for the categorical using the "categories" parameter
# Treat the categorical as ordered using the "ordered" parameter
catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
# Display the Categorical Index
print("Categorical Index...\n",catIndex)
# Get the categories
print("\nDisplayingCategories from CategoricalIndex...\n",catIndex.categories)
# Check categories for ordered relationship
print("\nDoes categories have ordered relationship...\n",catIndex.ordered)輸出
這將產生以下輸出 −
Categorical Index... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category') DisplayingCategories from CategoricalIndex... Index(['p', 'q', 'r', 's'], dtype='object') Does categories have ordered relationship... True
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP