I'm trying to create a running sum on a Dataframe that follows some conditions, sum all previous values with same 'first index current day' value and condiction is true, the pseudocode would be something like
df_ = df[first index current day:current index] #Delimiter dataframe range from begin of day until current row
df_ = df_[df_['toSum'] == True] #drop False
result = sum(df_['sales']) #Sum all sales until now with 'toSum' == True
This is very easy using a loop or even a apply but take too much time, my dataframe have more than 1000k rows, the best I could do until now is use threads but if it's possible to vectorize that algorithm it would be a lot fastter
The result is something like this:
| Actual Id | First ID Actual Day | Sales | toSum | AcumulativeDay |
|---|---|---|---|---|
| 0 | 0 | 1 | True | 1 |
| 1 | 0 | 1 | True | 2 |
| 2 | 0 | 1 | False | 2 |
| 3 | 0 | 1 | False | 2 |
| 4 | 0 | 1 | True | 3 |
| 5 | 5 | 1 | True | 1 |
| 6 | 5 | 1 | True | 2 |
| 7 | 5 | 1 | False | 2 |
| 8 | 5 | 1 | False | 2 |
| 9 | 5 | 1 | True | 3 |