CategoricalIndex से निर्दिष्ट श्रेणियों को निकालने के लिए, remove_categories() का उपयोग करें पंडों में विधि।
सबसे पहले, आवश्यक पुस्तकालयों को आयात करें -
import pandas as pd
"श्रेणियों" पैरामीटर का उपयोग करके श्रेणीबद्ध के लिए श्रेणियां सेट करें। "आदेशित" पैरामीटर -
. का उपयोग करके श्रेणीबद्ध के रूप में आदेश दिया गया व्यवहार करें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')