简体   繁体   English

如何获得条件累计和

[英]How Do I Get Conditional Cumulative Sum

I have a column that requires conditional cumulative sum where a null value resets the cumulation.我有一列需要条件累积和,其中 null 值重置累积。 How do I achieve this?我如何实现这一目标?

t = pd.DataFrame([2, 3, None, 1, 4])

# DataFrame
| 0    |
|------|
| 2    |
| 3    |
| NaN  |
| 1    |
| 4    |

# Desired Output
| 0    |
|------|
| 2    |
| 5    |
| NaN  |
| 1    |
| 5    |

Like this:像这样:

In [29]: t[0].groupby(t[0].isna().cumsum()).cumsum()                                                                                                                                                        
Out[29]: 
0    2.0
1    5.0
2    NaN
3    1.0
4    5.0
Name: 0, dtype: float64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM