Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat Rows in Data Frame n Times [duplicate]

Tags:

python

pandas

consider a data frame defined like so:

import pandas as pd
test = pd.DataFrame({
    'id' : ['a', 'b', 'c', 'd'],
    'times' : [2, 3, 1, 5]
})

Is it possible to create a new data frame from this in which each row is repeated times times, such that the result looks like this:

>>> result
   id  times
0   a      2
1   a      2
2   b      3
3   b      3
4   b      3
5   c      1
6   d      5
7   d      5
8   d      5
9   d      5
10  d      5
like image 334
93i7hdjb Avatar asked Mar 02 '18 17:03

93i7hdjb


People also ask

How do you repeat rows n times?

In the popping dialog, choose Copy and insert rows option in the Type section, then choose the range that you want to repeat to Insert Range textbox, and choose the column that decides the repeat times to the Repeat Times textbox. Click Ok. Then the rows will be repeated by the selected column.

How do I duplicate rows n times in R?

The way to repeat rows in R is by using the REP() function. The REP() function is a generic function that replicates the value of x one or more times and it has two mandatory arguments: x: A vector.

How do you repeat a row multiple times in python?

In Python, if you want to repeat the elements multiple times in the NumPy array then you can use the numpy. repeat() function. In Python, this method is available in the NumPy module and this function is used to return the numpy array of the repeated items along with axis such as 0 and 1.

How do you repeat a line in R?

To repeat one or more lines: Type R in the line command field of the line that is to be repeated. If you want to repeat the line more than once, type a number that is greater than 1 immediately after the R command. Press Enter.


Video Answer


1 Answers

Use a combination of pd.DataFrame.loc and pd.Index.repeat

test.loc[test.index.repeat(test.times)]

  id  times
0  a      2
0  a      2
1  b      3
1  b      3
1  b      3
2  c      1
3  d      5
3  d      5
3  d      5
3  d      5
3  d      5

To mimic your exact output, use reset_index

test.loc[test.index.repeat(test.times)].reset_index(drop=True)

   id  times
0   a      2
1   a      2
2   b      3
3   b      3
4   b      3
5   c      1
6   d      5
7   d      5
8   d      5
9   d      5
10  d      5
like image 123
piRSquared Avatar answered Oct 24 '22 09:10

piRSquared