पैंडास में CategoricalIndex के श्रेणी कोड प्राप्त करने के लिए, codes प्रॉपर्टी का उपयोग करें। सबसे पहले, आवश्यक लाइब्रेरी आयात करें:
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("\nCategory codes from CategoricalIndex...\n", catIndex.codes)
उदाहरण
निम्नलिखित कोड देखें:
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("\nCategory codes from CategoricalIndex...\n", catIndex.codes)
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
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') Category codes from CategoricalIndex... [0 1 2 3 0 1 2 3]