Change language

Python | Pandas Series.repeat ()

|

Series.repeat() Pandas Series.repeat() repeats the elements of the Series. It returns a new series in which each element of the current series repeats a specified number of times.

Syntax: Series.repeat (repeats, axis = None)

Parameter:
repeats: The number of repetitions for each element.
axis: None

Returns: repeated_series

Example # 1: Use Series.repeat () to repeat each value in this Series object 2 times.

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ 10 , 25 , 3 , 11 , 24 , 6 ])

 
# Create index

index_ = [ ’Coca Cola’ , ’Sprite’ , ’ Coke’ , ’Fanta’ , ’ Dew’ , ’ThumbsUp’ ]

 
# install and index

sr.index = index_

 
# Print series

print (sr)

Output:

Now we will use Series.repeat () to repeat each value of a given series object 2 times.

# repeat twice

result = sr.repeat (repeats = 2 )

 
# Print result

print (result)

Output:

As we can see in the output, Series.repeat () returned a new series object, where each value is repeated the specified number of times.

Example # 2: Use Series.repeat () to repeat each value in a given Series object 3 times.

# import pandas as pd

import pandas as pd

 
# Create a series

sr = pd.Series ([ ’New York’ , ’ Chicago’ , ’Toronto’ , ’ Lisbon’ , ’Rio’ ])

 
# Create index

index_ = [ ’ City 1’ , ’City 2’ , ’ City 3’ , ’City 4’ , ’City 5’

  
# set index

sr.index  = index_

 
# Print series

print (sr)

Output:

We will now use Series.repeat () to repeat each value of the given object series 3 times.

# repeat twice

result = sr.repeat (repeats = 3 )

 
# Print result

print (result)

Output:

As we can see in the output, Series.repeat () returned a new series object, where each value is repeated the specified number of times.

Python | Pandas Series.repeat () Python functions: Questions

Python | Pandas Series.repeat () repeat: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method