python - Pandas - cumsum by month? -


i have dataframe looks this:

date          n 2014-02-27    4 2014-02-28    5 2014-03-01    1 2014-03-02    6 2014-03-03    7 

i'm trying 1 looks this

date          n    csn 2014-02-27    4    4 2014-02-28    5    9 2014-03-01    1    1 2014-03-02    6    7 2014-03-03    7    14 

...i.e. want column running total within month , want start on each month. how can this?

use .groupby(), don't group month, groupby year-month instead. or else 2013-02 in same group 2014-02, etc.

in [96]:  df['month']=df['date'].apply(lambda x: x[:7]) in [97]:  df['csn']=df.groupby(['month'])['n'].cumsum() in [98]:  print df          date  n    month  csn 0  2014-02-27  4  2014-02    4 1  2014-02-28  5  2014-02    9 2  2014-03-01  1  2014-03    1 3  2014-03-02  6  2014-03    7 4  2014-03-03  7  2014-03   14  [5 rows x 4 columns] 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -