努力创造新系列

时间:2017-04-28 15:14:49

标签: python pandas

我有以下df:

                A   C
Date        
2015-06-29   196.0  1
2015-09-18   255.0  2
2015-08-24   236.0  3
2014-11-20    39.0  4
 2014-10-02    4.0  5

如何生成一个新系列,该系列是列c的所有先前行的总和?

这将是所需的输出:

D

1 
  #This second row of value 3 is the sum of first and second row of column c
3
 #This third row of value 6 is the sum of first, second and third row 
  value of column c , and so on
6
10
15

我尝试了一个循环,例如:

for j in range (len(df)):
new_series.iloc[j]+=df['C'].iloc[j]
return new_series

但似乎不起作用

2 个答案:

答案 0 :(得分:3)

IIUC您可以使用cumsum执行此操作:

In [373]:    
df['C'].cumsum()

Out[373]:
Date
2015-06-29     1
2015-09-18     3
2015-08-24     6
2014-11-20    10
2014-10-02    15
Name: C, dtype: int64

答案 1 :(得分:1)

Numpy替代品:

In [207]: np.add.accumulate(df['C'])
Out[207]:
2015-06-29     1
2015-09-18     3
2015-08-24     6
2014-11-20    10
2014-10-02    15
Name: C, dtype: int64

In [208]: np.cumsum(df['C'])
Out[208]:
2015-06-29     1
2015-09-18     3
2015-08-24     6
2014-11-20    10
2014-10-02    15
Name: C, dtype: int64

In [209]: df['C'].values.cumsum()
Out[209]: array([ 1,  3,  6, 10, 15], dtype=int64)