Open In App

Python | Pandas Index.repeat()

Last Updated : 18 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.repeat() function repeat elements of an Index. The function returns a new index where each element of the current index is repeated consecutively a given number of times.
Syntax: Index.repeat(repeats, *args, **kwargs) Parameters : repeats : The number of repetitions for each element. **kwargs : Additional keywords have no effect but might be accepted for compatibility with numpy. Returns : Newly created Index with repeated elements.
Example #1: Use Index.repeat()() function to repeat the elements of the index 2 times. Python3
# importing pandas as pd
import pandas as pd

# Creating the index
idx = pd.Index(['Beagle', 'Pug', 'Labrador', 'Pug',
                        'Mastiff', None, 'Beagle'])

# Print the Index
idx
Output : Let's repeat the index elements 2 times. Python3
# to repeat the values
idx.repeat(2)
Output : As we can see in the output, the function has returned a new index with all the values repeated 2 times. One important thing to notice is that the function has also repeated the NaN value. Example #2: Use Index.repeat() function to repeat the value of index 3 times. Python3
# importing pandas as pd
import pandas as pd

# Creating the index
idx = pd.Index([22, 14, 8, 56, None, 21, None, 23])

# Print the Index
idx
Output : Let's repeat the index elements 3 times. Python3
# to repeat the values
idx.repeat(3)
Output : As we can see in the output, the function has returned a new index with all the values repeated 3 times. One important thing to notice is that the function has also repeated the NaN value.

Next Article

Similar Reads