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')

更新于:2021-10-18

超过 3K 的浏览量

开启你的事业

完成课程获得认证

开始
广告