pandas -每月累积量? [英] Pandas - cumsum by month?

查看:54
本文介绍了 pandas -每月累积量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据框:

I have a dataframe that looks like 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 to get to one that looks like 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. I want a column with the running total within the month and I want it to start over each month. How can I do this?

推荐答案

使用 .groupby(),但不仅仅是按月分组, groupby 年月反而.否则 2013-02 将与 2014-02 等位于同一组中.

Use .groupby(), but don't just group by month, groupby year-month instead. Or else 2013-02 will be in the same group as 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]

这篇关于 pandas -每月累积量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆