KVA
KVA

Reputation: 49

Restart cumulative sum in pandas dataframe

I am trying to start a cumulative sum in a pandas dataframe, restarting everytime the absolute value is higher than 0.009. Could give you a excerpt of my tries but I assume they would just distract you. Have tried several things with np.where but at a certain point they start to overlap and it takes wrong things out.

Column b is the desired output.

df = pd.DataFrame({'values':(49.925,49.928,49.945,49.928,49.925,49.935,49.938,49.942,49.931,49.952)})
df['a']=df.diff()

 values      a      b
0  49.925    NaN  0.000
1  49.928  0.003  0.003
2  49.945  0.017  0.020 (restart cumsum next row)
3  49.928 -0.017 -0.017 (restart cumsum next row)
4  49.925 -0.003 -0.003
5  49.935  0.010  0.007
6  49.938  0.003  0.010 (restart cumsum next row)
7  49.942  0.004  0.004
8  49.931 -0.011 -0.007
9  49.952  0.021  0.014 (restart cumsum next row)

So the actual objective is for python to understand that I want to restart the cumulative sum when it exceeds the absolute value of 0.009

Upvotes: 1

Views: 871

Answers (1)

Rich Andrews
Rich Andrews

Reputation: 1670

I couldn't solve this in a vectorized manner, however applying a stateful function appears to work.

import pandas as pd
from pandas.compat import StringIO
print(pd.__version__)

df = pd.DataFrame({'values':(49.925,49.928,49.945,49.928,49.925,49.935,49.938,49.942,49.931,49.952)})
df['a']=df.diff()

accumulator = 0.0
reset = False
def myfunc(x):
    global accumulator, reset
    if(reset):
        accumulator = 0.0
        reset = False
    accumulator += x 
    if abs(accumulator) > .009: 
        reset = True
    return accumulator

df['a'].fillna(value=0, inplace=True)
df['b'] = df['a'].apply(myfunc)
print(df)

Produces

0.24.2
   values      a      b
0  49.925  0.000  0.000
1  49.928  0.003  0.003
2  49.945  0.017  0.020
3  49.928 -0.017 -0.017
4  49.925 -0.003 -0.003
5  49.935  0.010  0.007
6  49.938  0.003  0.010
7  49.942  0.004  0.004
8  49.931 -0.011 -0.007
9  49.952  0.021  0.014

Upvotes: 3

Related Questions