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