पहले से बनाए गए इंडेक्स ऑब्जेक्ट के लिए इंडेक्स नाम सेट करने के लिए, index.set_names() . का उपयोग करें पंडों में विधि। सबसे पहले, आवश्यक पुस्तकालयों को आयात करें -
import pandas as pd
पांडा इंडेक्स बनाना -
index = pd.Index(["Electronics", "Mobile Phones", "Accessories", "Home Decor", "Books"])
पांडा सूचकांक प्रदर्शित करें -
print("Pandas Index...\n",index) इंडेक्स का नाम सेट करें -
print("\nSet the index name...\n",index.set_names('Products'))
उदाहरण
निम्नलिखित कोड है -
import pandas as pd
# Creating Pandas index
index = pd.Index(["Electronics", "Mobile Phones", "Accessories", "Home Decor", "Books"])
# Display the Pandas index
print("Pandas Index...\n",index)
# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)
# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)
# set the name of index
print("\nSet the index name...\n",index.set_names('Products')) आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
Pandas Index... Index(['Electronics', 'Mobile Phones', 'Accessories', 'Home Decor', 'Books'], dtype='object') Number of elements in the index... 5 The dtype object... object Set the index name... Index(['Electronics', 'Mobile Phones', 'Accessories', 'Home Decor', 'Books'], dtype='object', name='Products')