Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

Python Pandas CategoricalIndex - जांचें कि श्रेणियों में क्रमबद्ध संबंध है या नहीं

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 के साथ बनाया गया था।

  1. - पायथन - जांचें कि पैंडास इंडेक्स ऑब्जेक्ट dtype का है या नहीं

    जांचने के लिए कि पैंडास इंडेक्स ऑब्जेक्ट dtype का है या नहीं, index.is_object() मेथड का उपयोग करें। सबसे पहले आवश्यक लाइब्रेरी इम्पोर्ट करें: import pandas as pd पैंडास इंडेक्स बनाएं: index = pd.Index([Electronics, 6, 10.5, Accessories, 25.6, 30]) पैंडास इंडेक्स प्रदर्शित करें: print(Pandas Index...

  1. - पायथन - जांचें कि पैंडास इंडेक्स फ्लोटिंग टाइप का है या नहीं

    जांचने के लिए कि पैंडास इंडेक्स फ्लोटिंग टाइप का है या नहीं, पैंडास में index.is_floating() मेथड का उपयोग करें। सबसे पहले आवश्यक लाइब्रेरी इम्पोर्ट करें: import pandas as pd पैंडास इंडेक्स बनाएं: index = pd.Index([5.7, 6.8, 10.5, 20.4, 25.6, 30.8, 40.5, 50.2]) पैंडास इंडेक्स प्रदर्शित करें: print(Pa

  1. - पायथन पैंडास - दिए गए टाइमस्टैम्प को मासिक फ्रीक्वेंसी के साथ पीरियड में बदलें

    दिए गए टाइमस्टैम्प को पीरियड में बदलने के लिए timestamp.to_period() मेथड का उपयोग करें। इसके भीतर freq पैरामीटर का उपयोग करके फ्रीक्वेंसी सेट करें। मासिक फ्रीक्वेंसी के लिए freq को M के रूप में सेट करें। सबसे पहले आवश्यक लाइब्रेरी इम्पोर्ट करें: import pandas as pd पैंडास में टाइमस्टैम्प ऑब्जेक्ट से