How to reset cumsum after change in sign of values?

孤街醉人 提交于 2019-12-02 11:22:38

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!