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:

values_to_repeat = [0.33, 0.28, 0.18]
df1 = pd.DataFrame({'values_to_repeat':values_to_repeat})

enter image description here

The second one is the following:

values_to_call = [0,1,2,0,0,0]
df2 = pd.DataFrame({'values_to_call':values_to_call})

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

import pandas as pd

values_to_call = [0,1,2,0,0,0]

def cascade(values_to_call):
    values_to_repeat = [0.33, 0.28, 0.18]
    list = []
    column = []
    for i in values_to_call:
        iteration_list = []
        for k in range(i):
            list.append(values_to_repeat[0])
        iteration_list += list # .append(list) doesn't work for some reason :/
        while values_to_repeat[-1] in list: list.remove(values_to_repeat[-1])
        for k in range(len(list)):
            list[k]=values_to_repeat[values_to_repeat.index(list[k])+1]
        column.append(iteration_list)
    return column

result = cascade(values_to_call)
data = {'values_to_call':values_to_call,'cascade_values':result}
df = pd.DataFrame(data)

df.to_csv('link to a file')

Result example

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