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