पैंडास में इंडेक्स के एलिमेंट्स को दोहराने के लिए index.repeat() मेथड का उपयोग करें। आर्गुमेंट के रूप में दोहराव की संख्या सेट करें। सबसे पहले आवश्यक लाइब्रेरी इम्पोर्ट करें:
import pandas as pd
पैंडास इंडेक्स बनाएं:
index = pd.Index(['Car','Bike','Airplane', 'Ship','Truck','Suburban'], name ='Transport')
पैंडास इंडेक्स प्रदर्शित करें:
print("Pandas Index...\n",index)
इंडेक्स के एलिमेंट्स को दोहराएं:
print("\nResult after repeating each index element twice...\n",index.repeat(2))
उदाहरण
निम्न कोड देखें:
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("\nResult after repeating each index element twice...\n",index.repeat(2))
आउटपुट
यह निम्न आउटपुट देगा:
Pandas Index... Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck', 'Suburban'], dtype='object', name='Transport') Number of elements in the index... 6 The dtype object... object Result after repeating each index element twice... Index(['Car', 'Car', 'Bike', 'Bike', 'Airplane', 'Airplane', 'Ship', 'Ship', 'Truck', 'Truck', 'Suburban', 'Suburban'], dtype='object', name='Transport')