
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Repeat elements of an Index
To repeat elements of an Index, use the index.repeat() method in Pandas. Set the number of repetitions as an argument. At first, import the required libraries −
import pandas as pd
Creating Pandas index −
index = pd.Index(['Car','Bike','Airplane', 'Ship','Truck','Suburban'], name ='Transport')
Display the Pandas index −
print("Pandas Index...\n",index)
Repeat elements of the index −
print("\nResult after repeating each index element twice...\n",index.repeat(2))
Example
Following is the code −
import pandas as pd # Creating Pandas index index = pd.Index(['Car','Bike','Airplane', 'Ship','Truck','Suburban'], name ='Transport') # 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) # repeat elements of the index print("\nResult after repeating each index element twice...\n",index.repeat(2))
Output
This will produce the following output −
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')
- Related Articles
- Python - Make new Pandas Index with deleting multiple index elements
- Python Pandas - Return number of unique elements in the Index object
- Python Pandas - Return the Number of elements in the underlying Index data
- Python Pandas - Set index name for an already created Index object
- Python Pandas - Create an Index based on an underlying Categorical
- Python Pandas - Check if the index is empty with 0 elements
- Python Pandas - Return whether all elements in the index are True
- Python Pandas - Return a new Index with elements of index not in other and get the difference
- Python Pandas - Return a new Index with elements of index not in other but unsort the result
- Python - Index Directory of Elements
- Python - Index Ranks of Elements
- Python Program to repeat elements at custom indices
- Python Pandas - Return an Index of formatted strings specified by date format
- Python Pandas - Alter index name
- Python - Repeat each element of a Pandas Series in a dissimilar way

Advertisements