Python | Pandas Index.repeat()
Last Updated :
18 Dec, 2018
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.
import pandas as pd
idx = pd.Index([ 'Beagle' , 'Pug' , 'Labrador' , 'Pug' ,
'Mastiff' , None , 'Beagle' ])
idx
|
Output :
Let’s repeat the index elements 2 times.
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.
import pandas as pd
idx = pd.Index([ 22 , 14 , 8 , 56 , None , 21 , None , 23 ])
idx
|
Output :
Let’s repeat the index elements 3 times.
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...