Python Pandas - 从 CategoricalIndex 中移除指定的分组
要从 CategoricalIndex 中移除指定的类别,请在 Pandas 中使用 remove_categories() 方法。
首先,导入所需的库 −
import pandas as pd
使用 "categories" 参数为类别设置类别,使用 "ordered" 参数将类别视为有序 −
catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
使用 remove_categories() 移除类别,将要移除的类别作为参数进行设置。原本属于已被移除类别的值将被设为 NaN −
print("\nCategoricalIndex after removing specified categories...\n",
catIndex.remove_categories(["p", "q"]))示例
以下为代码示例 −
import pandas as pd
# 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 CategoricalIndex
print("CategoricalIndex...\n",catIndex)
# Get the categories
print("\nDisplaying Categories from CategoricalIndex...\n",catIndex.categories)
# Remove categories using remove_categories()
# Set the categories to be removed as a parameter
# Values which were in the removed categories will be set to NaN
print("\nCategoricalIndex after removing specified categories...\n",
catIndex.remove_categories(["p", "q"]))输出
这将产生以下输出结果 −
CategoricalIndex... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category') Displaying Categories from CategoricalIndex... Index(['p', 'q', 'r', 's'], dtype='object') CategoricalIndex after removing specified categories... CategoricalIndex([nan, nan, 'r', 's', nan, nan, 'r', 's'], categories=['r', 's'], ordered=True, dtype='category')
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP