How to compute cumulative sum of previous N rows in pandas?

后端 未结 3 942
青春惊慌失措
青春惊慌失措 2020-12-09 09:16

I am working with pandas, but I don\'t have so much experience. I have the following DataFrame:

          A
0       NaN
1      0.00
2      0.00
3      3.33
4         


        
3条回答
  •  粉色の甜心
    2020-12-09 09:53

    Check the pandas.Series.expanding. The series.expanding(min_periods=2).sum()

    will do the job for you. And don't forget to set 0-th element, since it is NaN. I mean,

    accumulation = series.expanding(min_periods=2).sum()
    accumulation[0] = series[0] # or as you like
    

提交回复
热议问题