Pandas में CategoricalIndex की श्रेणियों को अक्रमबद्ध सेट करने के लिए as_unordered() मेथड का उपयोग करें।
आवश्यक लाइब्रेरी इम्पोर्ट करें
import pandas as pd
CategoricalIndex बनाएं
"categories" पैरामीटर का उपयोग करके श्रेणियां सेट करें। "ordered" पैरामीटर के साथ True मानकर श्रेणी को क्रमबद्ध मानें।
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
# श्रेणियां सेट करें और क्रमबद्ध मानें
catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
# CategoricalIndex प्रदर्शित करें
print("CategoricalIndex...\n", catIndex)
# श्रेणियां प्राप्त करें
print("\nDisplaying Categories from CategoricalIndex...\n", catIndex.categories)
# श्रेणियों को अक्रमबद्ध सेट करें
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')
व्याख्या: as_unordered() मेथड श्रेणियों को अक्रमबद्ध (ordered=False) सेट करके नया CategoricalIndex लौटाता है।