w3resource

Pandas Series: repeat() function

Repeat elements of a Pandas series

The repeat() function is used to repeat elements of a Series.

Returns a new Series where each element of the current Series is repeated consecutively a given number of times.

Syntax:

Series.repeat(self, repeats, axis=None)
Pandas Series repeat image
Name Description Type/Default Value Required / Optional
repeats The number of repetitions for each element. This should be a non-negative integer. Repeating 0 times will return an empty Series. int or array of ints Required
axis Must be None. Has no effect but is accepted for compatibility with numpy. Default Value: None Required

Returns: Series- Newly created Series with repeated elements.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['p', 'q', 'r'])
s

Output:

0    p
1    q
2    r
dtype: object
Pandas Series repeat image

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['p', 'q', 'r'])
s.repeat(2)

Output:

0    p
0    p
1    q
1    q
2    r
2    r
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['p', 'q', 'r'])
s.repeat([2, 3, 4])

Output:

0    p
0    p
1    q
1    q
1    q
2    r
2    r
2    r
2    r
dtype: object

Previous: Find indices where elements should be inserted to maintain order
Next: Squeeze 1 dimensional axis objects into scalars



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-repeat.php