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