I have a dataframe with a column having either +1, 0 or -1. I would like to do the cumsum() keeping the result between -3 and +3. Example: for the series, [+1, +1, +1, +1, 0, -1], I am expecting the result [+1, +2, +3, +3, +3, +2]. Any clue? Thx a lot.
Asked
Active
Viewed 166 times
2
-
There is likely not a single function that does this since it's a pretty niche requirement. Maybe try writing your own function and if you run into a proble, ask a specific question about it? Please take the [tour], read [what's on-topic here](/help/on-topic), [ask], and the [question checklist](//meta.stackoverflow.com/q/260648/843953), and how to provide a [mre]. Welcome to Stack Overflow! – Pranav Hosangadi Sep 20 '21 at 15:51
-
2Related: https://stackoverflow.com/questions/53104756/numpy-custom-cumsum-function-with-upper-lower-limits – Pranav Hosangadi Sep 20 '21 at 15:53
-
Related : https://stackoverflow.com/questions/56904390/restart-cumsum-and-get-index-if-cumsum-more-than-value/56905635#56905635 – anky Sep 20 '21 at 16:05
-
Thx guys -- eventually I used Pranav suggestion and did it using one loop on the vector I am interested in summing up. Performance wise ok. – Greg Sep 21 '21 at 05:45