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

Python Pandas - दो CategoricalIndex ऑब्जेक्ट में समान तत्व हैं या नहीं जांचें

Pandas में दो CategoricalIndex ऑब्जेक्ट में समान तत्व हैं या नहीं निर्धारित करने के लिए equals() मेथड का उपयोग करें।

आवश्यक लाइब्रेरी इम्पोर्ट करें

import pandas as pd

दो CategoricalIndex ऑब्जेक्ट बनाएं

"categories" पैरामीटर का उपयोग करके श्रेणियां सेट करें। "ordered" पैरामीटर का उपयोग करके श्रेणी को क्रमबद्ध मानें।

catIndex1 = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
catIndex2 = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])

दोनों CategoricalIndex ऑब्जेक्ट की समानता जांचें

print("\nCheck both the CategoricalIndex objects for equality...\n", catIndex1.equals(catIndex2))

पूरा उदाहरण

import pandas as pd

# श्रेणियां सेट करें और क्रमबद्ध मानें
catIndex1 = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
catIndex2 = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])

# CategoricalIndex ऑब्जेक्ट प्रदर्शित करें
print("CategoricalIndex1...\n", catIndex1)
print("\nCategoricalIndex2...\n", catIndex2)

# दोनों CategoricalIndex ऑब्जेक्ट की समानता जांचें
print("\nCheck both the CategoricalIndex objects for equality...\n", catIndex1.equals(catIndex2))

आउटपुट

CategoricalIndex1... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category')
CategoricalIndex2... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category')
Check both the CategoricalIndex objects for equality... True

व्याख्या: दोनों CategoricalIndex में समान मान, समान श्रेणियां और समान क्रम है, इसलिए equals() True लौटाता है।

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

    जांचने के लिए कि पैंडास इंडेक्स इंटरवल ऑब्जेक्ट्स रखता है या नहीं, पैंडास में index.is_interval() मेथड का उपयोग करें। सबसे पहले आवश्यक लाइब्रेरी इम्पोर्ट करें: import pandas as pd इंटरवल ऑब्जेक्ट्स बनाएं: interval1 = pd.Interval(10, 30) interval2 = pd.Interval(30, 50) इंटरवल्स प्रदर्शित करें: print(

  1. - पायथन प्रोग्राम: Series से ठीक दो स्पेस वाले तत्व हटाएं

    समस्या विवरण स्ट्रिंग Series से उन तत्वों को हटाएं जिनमें ठीक दो स्पेस (spaces) हों। इनपुट Series 0 This is pandas 1 python script 2 pandas series dtype: object अपेक्षित आउटपुट 1 python script 2 pandas series dtype: object समाधान 1: Regex और filter (सुझावित) import pandas as pd imp

  1. - पायथन में दो सूचियों के तत्वों की तुलना कैसे करें?

    नोट: यह आर्टिकल पायथन 2 के cmp() फ़ंक्शन पर आधारित है जो पायथन 3 में हटा दिया गया है। पायथन 3 में सूचियों की तुलना के लिए अलग तरीके उपयोग किए जाते हैं। cmp() मेथड दो सूचियों के तत्वों की तुलना करता है। यदि तत्व एक ही प्रकार के हैं, तो यह तुलना करता है और परिणाम वापस करता है। यदि तत्व अलग-अलग प्रकार