close

How can I replicate rows in Pandas?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How can I replicate rows in Pandas in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How can I replicate rows in Pandas?

  1. How can I replicate rows in Pandas?

    These will repeat the indices and preserve the columns as op demonstrated

  2. I replicate rows in Pandas

Method 1

These will repeat the indices and preserve the columns as op demonstrated

iloc version 1

df.iloc[np.arange(len(df)).repeat(3)]

iloc version 2

df.iloc[np.arange(len(df) * 3) // 3]

Method 2

You can try the following code:

df = df.iloc[df.index.repeat(3),:].reset_index()

df.index.repeat(3) will create a list where each index value will be repeated 3 times and df.iloc[df.index.repeat(3),:] will help generate a dataframe with the rows as exactly returned by this list.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read