Skip to content
Advertisement

Is there any python way to repeat values of a row combining them in a column with a list type?

I have two dataframes, the first is this one, created with the following code:

JavaScript

enter image description here

The second one is the following:

JavaScript

enter image description here

What I need to do is to add a column to the second data frame that contains lists of values taken from the first data frame. I will show it and then explain it with words:

enter image description here

The lists should contain the first value of the column “values_to_repeat” repeated for the value of “values_to_call”, and then in the following 2 rows, it should take the second and the third value, until disappearing in row = (i + 3).

Explaining it row by row:

1) there is an empty list because there is a 0 in “value_to_call”

2) there is [0.33] because there is a 1 in “values_to_call”

3) there is the list [0.28, 0.33, 0.33] where the first value is the value of row number 2 that decreased to 0.28 and then 2 new 0.33 because there is a 2 in “values_to_call”

4) there is the list [0.18, 0.28, 0.28] where the first value is the value of row number 2 that decreased to 0.18 and the other 2 values are the values generated in row number 3 that decreased to 0.28

5) there is the list [0.18, 0.18], now the value generated in row number 2, after decreasing 2 times it disappeard, while the 2 values generated in row number 3 decrease to 0.18

6) there is an empty list because after appearing in i, i+1 and i+2, at i+3 the values generated in row number 3 disappeared as well

Advertisement

Answer

My solution for any “values_to_repeat” and any “values_to_call”>0

JavaScript

Result example

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement