[英]Pandas sum negative values with previous value if positive
It is bit hard to put my problem in words.用语言来表达我的问题有点困难。 I have a dataframe with positive and negative values.
我有一个具有正值和负值的 dataframe。
2012-01-01 58.0
2012-06-01 8.0
2012-07-01 10.0
2013-01-01 50.0
2013-02-01 -6.0
2013-03-01 -8.0
2013-04-01 20.0
2013-07-01 3.0
2013-12-01 0.0
2014-02-01 88.0
2014-03-01 -40.0
I want to sum a negative value in a row with the previous row value if it is positive until no negatives are left.如果它是正数,我想将一行中的负值与前一行值相加,直到没有负数。
For example: the final list should be: [58,8,10, 50+(-6-8),20.0, 3.0, 0.0, 88+(-40)]
例如:最终列表应该是:
[58,8,10, 50+(-6-8),20.0, 3.0, 0.0, 88+(-40)]
2012-01-01 58.0
2012-06-01 8.0
2012-07-01 10.0
2013-01-01 36.0
2013-04-01 20.0
2013-07-01 3.0
2013-12-01 0.0
2014-02-01 48.0
The dataframe is huge so i would really prefer a pandas
solution. dataframe 很大,所以我真的更喜欢
pandas
解决方案。
You can identify the negative blocks with cumsum
, and use that for groupby:您可以使用
cumsum
识别负块,并将其用于 groupby:
(df.groupby(df['value'].ge(0).cumsum(), as_index=False)
.agg({'date':'first','value':'sum'})
)
Output: Output:
date value
0 2012-01-01 58.0
1 2012-06-01 8.0
2 2012-07-01 10.0
3 2013-01-01 36.0
4 2013-04-01 20.0
5 2013-07-01 3.0
6 2013-12-01 0.0
7 2014-02-01 48.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.