CategoricalIndex की श्रेणियों को अनियंत्रित करने के लिए, as_unordered() का उपयोग करें पंडों में विधि।
सबसे पहले, आवश्यक पुस्तकालयों को आयात करें -
import pandas as pd
"श्रेणियों" पैरामीटर का उपयोग करके श्रेणीबद्ध के लिए श्रेणियां सेट करें। "आदेशित" पैरामीटर का उपयोग करके श्रेणीबद्ध के रूप में आदेश दिया गया मान सही है -
catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
श्रेणियों को अनियंत्रित करने के लिए सेट करें -
print("\nCategoricalIndex unordered...\n", catIndex.as_unordered())
उदाहरण
निम्नलिखित कोड है -
import pandas as pd
# Set the categories for the categorical using the "categories" parameter
# Treat the categorical as ordered using the "ordered" parameter with value True
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)
# Set the categories to be unordered
print("\nCategoricalIndex unordered...\n", catIndex.as_unordered()) आउटपुट
यह निम्नलिखित आउटपुट देगा -
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 unordered... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=False, dtype='category')