pandas 如何计算pandas中前N行的累积总和?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43787059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:32:14  来源:igfitidea点击:

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

pythonpandascumsum

提问by lmiguelvargasf

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

我正在与Pandas一起工作,但我没有太多经验。我有以下数据帧:

          A
0       NaN
1      0.00
2      0.00
3      3.33
4     10.21
5      6.67
6      7.00
7      8.27
8      6.07
9      2.17
10     3.38
11     2.48
12     2.08
13     6.95
14     0.00
15     1.75
16     6.66
17     9.69
18     6.73
19     6.20
20     3.01
21     0.32
22     0.52

and I need to compute the cumulative sum of the previous 11 rows. When there is less than 11 previously, they remaining are assumed to be 0.

我需要计算前 11 行的累积总和。当之前少于 11 个时,它们剩余的被假定为 0。

        B
0     NaN
1    0.00
2    0.00
3    0.00
4    3.33
5    13.54
6    20.21
7    27.20
8    35.47
9    41.54
10    43.72
11   47.09
12   49.57 
13   51.65
14   58.60
15   58.60
16   57.02
17   53.48
18   56.49
19   56.22
20   54.16
21   51.10
22   49.24

I have tried:

我试过了:

df['B'] = df.A.cumsum().shift(-11).fillna(0)

However, this is not achieving what I want, but this is rotating the result of a cumulative sum. How can I achieve this?

但是,这并没有达到我想要的效果,而是旋转了累积总和的结果。我怎样才能做到这一点?

回答by EdChum

Call rollingwith min_periods=1and window=11and sum:

rollingmin_periods=1andwindow=11和 and调用sum

In [142]:
df['A'].rolling(min_periods=1, window=11).sum()

Out[142]:
0       NaN
1      0.00
2      0.00
3      3.33
4     13.54
5     20.21
6     27.21
7     35.48
8     41.55
9     43.72
10    47.10
11    49.58
12    51.66
13    58.61
14    55.28
15    46.82
16    46.81
17    49.50
18    47.96
19    48.09
20    48.93
21    45.87
22    43.91
Name: A, dtype: float64

回答by Mohammad Athar

you might have to do it the hard way

你可能不得不以艰难的方式去做

B = []
i =0
m_lim = 11
while i<len(A):
    if i<m_lim:
      B.append(sum(A[0:i]))
    if 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