I have a data frame that looks like this:
user1,product1,0 user1,product2,2 user1,product3,1 user1,product4,2 user2,product3,0 user2,product2,2 user3,product4,0 user3,product5,3
A data frame has millions of rows. I need to go through each row, and if the value in the last column is 0, save this product number, otherwise add the product number to the previous product number, which has value = 0, and then write in a new data frame.
For example, the resulting matrix should be
user1,product1 user1,product1product2 user1,product1product3 user1,product1product4 user2,product3 user2,product3product2 user3,product4 user3,product4product5
I wrote a for
loop to go through each line, and it works, but very slow. How can I speed it up? I tried to vectorize it, but I'm not sure, because I need to check the value of the previous line.