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 的瀏覽量

開啟你的事業

完成課程獲得認證

開始
廣告