python - Trying to generate a rolling_sum with Pandas -


i have simple dataframe regular samples:

dates = pd.date_range('20150101', periods=6) df = pd.dataframe([1,2,3,4,5,6], index=dates, columns=list('a')) df.loc[:,'b'] = 0 df.iloc[0,1] =10 df   out[119]:               b 2015-01-01  1   10 2015-01-02  2   0 2015-01-03  3   0 2015-01-04  4   0 2015-01-05  5   0 2015-01-06  6   0 

what using rolling_sum function in pandas create values in column b such following table:

              b 2015-01-01  1   11 2015-01-02  2   13 2015-01-03  3   16 2015-01-04  4   20 2015-01-05  5   25 2015-01-06  6   31 

that is, b(i+1) = a(i+1) + b(i) - note there special case @ b(0) there initial value of 10. i've tried using following, gives wrong answer:

df.b = pd.rolling_sum(df.a + df.b, 1) 

try expanding_sum:

in [15]: df out[15]:                b 2015-01-01  1  10 2015-01-02  2   0 2015-01-03  3   0 2015-01-04  4   0 2015-01-05  5   0 2015-01-06  6   0  in [16]: df.b = pd.expanding_sum(df.a) + df.b[0]  in [17]: df out[17]:                b 2015-01-01  1  11 2015-01-02  2  13 2015-01-03  3  16 2015-01-04  4  20 2015-01-05  5  25 2015-01-06  6  31 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -