Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

पायथन पैंडास - इंडेक्स के एलिमेंट्स को दोहराएं

पैंडास में इंडेक्स के एलिमेंट्स को दोहराने के लिए 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')
  1. - पायथन - सूची में आसन्न तत्व

    परिचय जब सूची में आसन्न तत्वों को प्रदर्शित करना हो, तो एक मेथड परिभाषित की जाती है जो enumerate और सरल पुनरावृत्ति का उपयोग करके परिणाम निर्धारित करती है। उदाहरण def find_adjacent_elements(my_list): my_result = [] for index, ele in enumerate(my_list): if index == 0: my

  1. - पायथन – समान इंडेक्स वाले तत्व

    जब समान इंडेक्स वाले तत्वों को प्रदर्शित करना हो, तो सरल पुनरावृत्ति और enumerate एट्रिब्यूट का उपयोग किया जाता है। उदाहरण my_list = [33, 1, 2, 45, 41, 13, 6, 9] print(सूची है:) print(my_list) my_result = [] for index, element in enumerate(my_list): if index == element: my_result.append

  1. - पायथन में दो सूचियों के इंडेक्स तत्वों को समान करें

    पायथन के साथ डेटा मैनिपुलेशन के दौरान, हमें दो सूचियों को एक साथ लाने और उनमें तत्वों को जोड़ीवार समान करने की आवश्यकता हो सकती है। जिसका मतलब है कि सूची 1 से इंडेक्स 0 का तत्व सूची 2 के इंडेक्स 0 के तत्व के साथ समान होगा और इसी तरह। tuple के साथ tuple फंक्शन का लाभ उठाया जाएगा ताकि प्रत्येक सूची स