जांचने के लिए कि पैंडास इंडेक्स कैटेगोरिकल डेटा रखता है या नहीं, पैंडास में index.is_categorical() मेथड का उपयोग करें। सबसे पहले आवश्यक लाइब्रेरी इम्पोर्ट करें:
import pandas as pd
astype() मेथड का उपयोग करके टाइप को category के रूप में सेट करके पैंडास इंडेक्स बनाएं:
index = pd.Index(["Electronics","Accessories","Furniture"]).astype("category")
पैंडास इंडेक्स प्रदर्शित करें:
print("Pandas Index...\n",index)
जांचें कि इंडेक्स कैटेगोरिकल डेटा रखता है या नहीं:
print("\nDoes Index holds categorical data?\n",index.is_categorical())
उदाहरण
निम्न कोड है:
import pandas as pd
# पैंडास इंडेक्स बनाना
index = pd.Index(["Electronics","Accessories","Furniture"]).astype("category")
# पैंडास इंडेक्स प्रदर्शित करना
print("Pandas Index...\n",index)
# इंडेक्स में एलिमेंट्स की संख्या लौटाएं
print("\nNumber of elements in the index...\n",index.size)
# डेटा का dtype लौटाएं
print("\nThe dtype...\n",index.dtype)
# जांचें कि इंडेक्स कैटेगोरिकल डेटा रखता है या नहीं
print("\nDoes Index holds categorical data?\n",index.is_categorical())
आउटपुट
यह निम्न आउटपुट देगा:
Pandas Index... CategoricalIndex(['Electronics', 'Accessories', 'Furniture'], categories=['Accessories', 'Electronics', 'Furniture'], ordered=False, dtype='category') Number of elements in the index... 3 The dtype... category Does Index holds categorical data? True