2
In [46]: d = np.random.randn(10, 1) * 2

In [47]: df = pd.DataFrame(d.astype(int), columns=['data'])

I am trying to create a cumsum column where it should reset after a sign change in data column, like this

   data  custom_cumsum
0    -2  -2
1    -1  -3 
2     1   1
3    -3  -3
4    -1  -4
5     2   2 
6     0   2 
7     3   5 
8    -1  -1 
9    -2  -3 

I am able to achieve this with df.iterrows(). I am trying to avoid iterrows and do it with vector operations. There are couple of questions on resetting cumsum when there is NaN. I am not able to achieve this cumsum with those solutions.

Drise
  • 4,310
  • 5
  • 41
  • 66
Chillar Anand
  • 27,936
  • 9
  • 119
  • 136

1 Answers1

7

Create new key to groupby, then do cumsum within each group

New key Create: By using the sign change , if change we add one then it will belong to nest group

df.groupby(df.data.lt(0).astype(int).diff().ne(0).cumsum()).data.cumsum()
Out[798]: 
0   -2
1   -3
2    1
3   -3
4   -4
5    2
6    2
7    5
8   -1
9   -3
Name: data, dtype: int64
BENY
  • 317,841
  • 20
  • 164
  • 234
  • 1
    That magic `diff().ne(0).cumsum()` sequence is genius! Worth memorizing. It'd be amazing if there's a book out there that teaches you functional snippets like that for data manipulation. – kfmfe04 Apr 27 '22 at 18:34