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 लौटाता है।