值的符号更改后如何重设累积金额? [英] How to reset cumsum after change in sign of values?
问题描述
In [46]: d = np.random.randn(10, 1) * 2
In [47]: df = pd.DataFrame(d.astype(int), columns=['data'])
我正在尝试创建一个cumsum列,在该列中应在数据列中的符号更改后重置,就像这样
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
我可以通过df.iterrows()
实现这一目标.我试图避免迭代,并通过向量运算来做到这一点.关于重置 cum (当存在NaN时).这些解决方案我无法实现这一目标.
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.
推荐答案
为groupby
创建新密钥,然后在每个组中进行cumsum
Create new key to groupby
, then do cumsum
within each group
新密钥创建:通过使用符号change,如果更改我们添加一个,则它将属于嵌套组
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
这篇关于值的符号更改后如何重设累积金额?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!