简体   繁体   English

在 pandas 中使用 groupby 绘制累积和

[英]Plotting a cumulative sum with groupby in pandas

I'm missing something really obvious or simply doing this wrong.我错过了一些非常明显的东西,或者只是做错了。 I have two dataframes of similar structure and I'm trying to plot a time-series of the cumulative sum of one column from both.我有两个结构相似的数据框,我正在尝试 plot 两者中一列的累积总和的时间序列。 The dataframes are indexed by date:数据框按日期索引:

df1
              value
2020-01-01    2435
2020-01-02    12847
...
2020-10-01    34751

The plot should be grouped by month and be a cumulative sum of the whole time range. plot 应该按月分组,并且是整个时间范围的累积和。 I've tried:我试过了:

line1 = df1.groupby(pd.Grouper(freq='1M')).value.cumsum()
line2 = df2.groupby(pd.Grouper(freq='1M')).value.cumsum()

and then plot, but it resets after each month.然后是 plot,但它会在每个月后重置。 How can I change this?我怎样才能改变这个?

I am guessing you want to group and take the mean or something to represent the cumulative value for each month, and plot:我猜你想分组并取平均值或其他东西来表示每个月的累积值,以及 plot:

df1 = pd.DataFrame({'value':np.random.randint(100,200,366)},
                   index=pd.date_range(start='1/1/2018', end='1/1/2019'))

df1.cumsum().groupby(pd.Grouper(freq='1M')).mean().plot()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM