Hi, I have a simple problem: I have a DataFrame that looks like this:
df = DataFrame()
df.sum = [[200,0,1,1,0], [0,0,0], [0,1,1,1,1,0,0]]
df.score= [[1,2,22,25,5], [1,1,2], [1,24,54,89,12,1,2]]
df.id = ["1", "2", "3"]
And I want to generate a column that has the values of df.score that coincide with the values that are >+ 1 in the df.sum column. This is my expected result:
df.x1 = [[1,22,25], [], [24,54,89,12]]
Where the second row should be empty, because there are no values greater than 1 in df.sum[1].
This is the code that I’ve been tinkering with (I took it from Change column to row using conditions), but I can’t get my head around on how to modify it so it works:
dv2 = df.sum;
for (i, v) in enumerate(dv2)
for (j,h) in enumerate(v)
if h >= 1
df[:,:new_col] .= df.Score[i][j]
end
end
end
The problem with this that it gives me a column that is composed only by the number 12 ([12, 12, 12]
) in every row, and that is the last value that is greater than 1 of the last column. I don’t know why this happens, but I know that it has to do with how I the loop is written
Any help is welcome, thanks a lot!