Reputation: 41
df
Name Run ID1 ID2
0 A 18 100 500
1 B 19 150 550
2 C 18 200 600
3 D 15 250 650
I then have a variable named max_runs = 20 What I want to do is get the data into this format below. Essentially copy each unique row max_runs - df['Run'] times
df_output
Name Run ID1 ID2
1 A 19 100 500
2 A 20 100 500
3 B 20 150 550
4 C 19 200 600
5 C 20 200 600
6 D 16 250 650
7 D 17 250 650
8 D 18 250 650
9 D 19 250 650
10 D 20 250 650
Thanks for any help and let me know if I need to explain further
Upvotes: 0
Views: 91
Reputation: 150785
You can use repeat
to repeat the rows and assign
to modify the new run:
(df.loc[df.index.repeat(20-df.Run)]
.assign(Run=lambda x: x.groupby(level=0).cumcount().add(x.Run+1))
.reset_index()
)
Output:
index Name Run ID1 ID2
0 0 A 19 100 500
1 0 A 20 100 500
2 1 B 20 150 550
3 2 C 19 200 600
4 2 C 20 200 600
5 3 D 16 250 650
6 3 D 17 250 650
7 3 D 18 250 650
8 3 D 19 250 650
9 3 D 20 250 650
Upvotes: 1