परिचय
पहले से बने पैंडास इंडेक्स ऑब्जेक्ट के लिए नाम सेट करने के लिए index.set_names() मेथड का उपयोग करें।
आवश्यक लाइब्रेरी आयात करें
import pandas as pd
पैंडास इंडेक्स बनाएं
index = pd.Index(["Electronics", "Mobile Phones", "Accessories", "Home Decor", "Books"])
पैंडास इंडेक्स प्रदर्शित करें
print("पैंडास इंडेक्स...\n", index)
इंडेक्स नाम सेट करें
print("\nइंडेक्स नाम सेट करें...\n", index.set_names('Products'))
पूर्ण उदाहरण
import pandas as pd
# पैंडास इंडेक्स बनाएं
index = pd.Index(["Electronics", "Mobile Phones", "Accessories", "Home Decor", "Books"])
# पैंडास इंडेक्स प्रदर्शित करें
print("पैंडास इंडेक्स...\n", index)
# इंडेक्स में तत्वों की संख्या लौटाएं
print("\nइंडेक्स में तत्वों की संख्या...\n", index.size)
# डेटा का dtype लौटाएं
print("\nDtype ऑब्जेक्ट...\n", index.dtype)
# इंडेक्स नाम सेट करें
print("\nइंडेक्स नाम सेट करें...\n", index.set_names('Products'))
आउटपुट
पैंडास इंडेक्स... Index(['Electronics', 'Mobile Phones', 'Accessories', 'Home Decor', 'Books'], dtype='object') इंडेक्स में तत्वों की संख्या... 5 Dtype ऑब्जेक्ट... object इंडेक्स नाम सेट करें... Index(['Electronics', 'Mobile Phones', 'Accessories', 'Home Decor', 'Books'], dtype='object', name='Products')
मुख्य बिंदु
set_names()नाम के साथ नया इंडेक्स लौटाता है।- मूल इंडेक्स अपरिवर्तित रहता है (इम्यूटेबल)।
- इनप्लेस नाम बदलने के लिए:
index.rename('Products', inplace=True)याindex.name = 'Products'। - मल्टीइंडेक्स के लिए नामों की सूची पास करें।