अंतर्निहित कैटेगोरिकल पर आधारित इंडेक्स बनाने के लिए, pandas.CategoricalIndex() विधि का उपयोग करें।
सबसे पहले, आवश्यक लाइब्रेरी आयात करें:
import pandas as pd
CategoricalIndex अंतर्निहित कैटेगोरिकल पर आधारित इंडेक्स है। 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)
# न्यूनतम मान प्राप्त करें
print("\nMinimum value from CategoricalIndex...\n", catIndex.min())
# अधिकतम मान प्राप्त करें
print("\nMaximum value from CategoricalIndex...\n", catIndex.max())
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
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') Minimum value from CategoricalIndex... p Maximum value from CategoricalIndex... s