Python | Pandas Index.repeat ()

Index.repeat() Pandas Index.repeat() repeats index items. The function returns a new index, where each element of the current index is repeated a specified 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 () () to repeat index items 2 times.

# import pandas as pd

import pandas as pd

 
# Create index

idx = pd.Index ([ `Beagle` , `Pug` , `Labrador` , ` Pug` ,

`Mastiff` , None , `Beagle` ])

 
# Print index
idx

Output:

Let`s repeat the index items 2 times.

 

# repeat values ​​

idx.repeat ( 2 )

Output:

As we can see in the output, the function returned a new index with all values ​​repeated 2 times. It`s important to note that the function also repeated the NaN value.

Example # 2: Use Index.repeat () to repeat index value 3 times.

# import pandas as pd

import pandas as pd

 
# Index creation

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

 
# Print index
idx

Output:

Let`s repeat the index items 3 times.

# repeat values ​​

idx.repeat ( 3 )

Exit:

As we can see in the output, the function returned a new index with all values, repeated 3 times. It`s important to note that the function also repeated the NaN value.