Repeat Rows in Data Frame n Times
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
Repeat rows of a data.frame N times
EDIT: updated to a better modern R answer.
You can use replicate()
, then rbind
the result back together. The rownames are automatically altered to run from 1:nrows.
d <- data.frame(a = c(1,2,3),b = c(1,2,3))
n <- 3
do.call("rbind", replicate(n, d, simplify = FALSE))
A more traditional way is to use indexing, but here the rowname altering is not quite so neat (but more informative):
d[rep(seq_len(nrow(d)), n), ]
Here are improvements on the above, the first two using purrr
functional programming, idiomatic purrr:
purrr::map_dfr(seq_len(3), ~d)
and less idiomatic purrr (identical result, though more awkward):
purrr::map_dfr(seq_len(3), function(x) d)
and finally via indexing rather than list apply using dplyr
:
d %>% slice(rep(row_number(), 3))
replicate rows by n times in python
Another method could be:
df.assign(Times = df.Times.apply(lambda x: range(1, x + 1))).explode('Times')
Out[]:
String Times
0 a 1
0 a 2
1 b 1
1 b 2
1 b 3
2 c 1
2 c 2
2 c 3
2 c 4
2 c 5
How can I replicate rows in Pandas?
Use np.repeat
:
Version 1:
Try using np.repeat
:
newdf = pd.DataFrame(np.repeat(df.values, 3, axis=0))
newdf.columns = df.columns
print(newdf)
The above code will output:
Person ID ZipCode Gender
0 12345 882 38182 Female
1 12345 882 38182 Female
2 12345 882 38182 Female
3 32917 271 88172 Male
4 32917 271 88172 Male
5 32917 271 88172 Male
6 18273 552 90291 Female
7 18273 552 90291 Female
8 18273 552 90291 Female
np.repeat
repeats the values of df
, 3
times.
Then we add the columns with assigning new_df.columns = df.columns
.
Version 2:
You could also assign the column names in the first line, like below:
newdf = pd.DataFrame(np.repeat(df.values, 3, axis=0), columns=df.columns)
print(newdf)
The above code will also output:
Person ID ZipCode Gender
0 12345 882 38182 Female
1 12345 882 38182 Female
2 12345 882 38182 Female
3 32917 271 88172 Male
4 32917 271 88172 Male
5 32917 271 88172 Male
6 18273 552 90291 Female
7 18273 552 90291 Female
8 18273 552 90291 Female
Repeat rows of a data.frame
df <- data.frame(a = 1:2, b = letters[1:2])
df[rep(seq_len(nrow(df)), each = 2), ]
Pandas: repeat dataframe n times
Use:
N = 3
df = pd.concat([df] * N, ignore_index=True)
print (df)
col
0 0
1 60
2 300
3 320
4 0
5 60
6 300
7 320
8 0
9 60
10 300
11 320
Repeat row of data table N times and join to another table in R
Do you mean something like this?
dt1[, id:=dt2$id]
Output:
|V1 |V2 |id |
|:--|:--|:-------------|
|a |a |c(4,98,56,32) |
|a |b |c(4,98,56,32) |
|a |c |c(4,98,56,32) |
|b |c |c(4,98,56,32) |
|c |d |c(4,98,56,32) |
Related Topics
R Shiny Passing Reactive to Selectinput Choices
How to Divide Each Row of a Matrix by Elements of a Vector in R
Dplyr: How to Use Group_By Inside a Function
Converting Decimal to Binary in R
Remove Columns With Zero Values from a Dataframe
Dplyr: Nonstandard Column Names (White Space, Punctuation, Starts With Numbers)
Displaying Text Below the Plot Generated by Ggplot2
Subset a Dataframe Between 2 Dates
What Does %≫% Function Mean in R
Dplyr Mutate/Replace Several Columns on a Subset of Rows
Subscript Out of Bounds - General Definition and Solution
Fitting Several Regression Models With Dplyr
Remove Na Values from a Vector
How R Formats Posixct With Fractional Seconds