इंडेक्स का नाम बदलने के लिए पैंडास में index.rename() मेथड का उपयोग करें। सबसे पहले आवश्यक लाइब्रेरी इम्पोर्ट करें:
import pandas as pd
पैंडास इंडेक्स बनाएं:
index = pd.Index(['Car','Bike','Airplane', 'Ship','Truck','Suburban'], name='Transport')
पैंडास इंडेक्स प्रदर्शित करें:
print("Pandas Index...\n",index)
इंडेक्स का नाम बदलें:
print("\nRename the index...\n",index.rename('Mode_of_Transport'))
उदाहरण
निम्न कोड है:
import pandas as pd
# पैंडास इंडेक्स बनाना
index = pd.Index(['Car','Bike','Airplane', 'Ship','Truck','Suburban'], name='Transport')
# पैंडास इंडेक्स प्रदर्शित करना
print("Pandas Index...\n",index)
# इंडेक्स में एलिमेंट्स की संख्या लौटाएं
print("\nNumber of elements in the index...\n",index.size)
# डेटा का dtype लौटाएं
print("\nThe dtype object...\n",index.dtype)
# इंडेक्स का नाम बदलें
print("\nRename the index...\n",index.rename('Mode_of_Transport'))
आउटपुट
यह निम्न आउटपुट देगा:
Pandas Index... Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck', 'Suburban'], dtype='object', name='Transport') Number of elements in the index... 6 The dtype object... object Rename the index... Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck', 'Suburban'], dtype='object', name='Mode_of_Transport')