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

后端 未结 3 952
青春惊慌失措
青春惊慌失措 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:50

    you might have to do it the hard way

    B = []
    i =0
    m_lim = 11
    while i=m_lim and i < len(A) -m_lim:
            B.append(sum(A[i-m_lim:i]))
        if i>= len(A) -m_lim:
          B.append(sum(A[i:]))
        i=i+1
    df['B'] = B
    

提交回复
热议问题