Pandas 绘制计数器随时间累积的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48739374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:09:39  来源:igfitidea点击:

Pandas plot cumulative sum of counters over time

pythonpandasmatplotlibplot

提问by milosgajdos

I have a following dataframe:

我有以下数据框:

    Joined      User ID
0   2017-08-19  user 182737081
1   2017-05-07  user 227151009
2   2017-11-29  user 227306568
3   2016-05-22  user 13661634
4   2017-01-23  user 220545735

I'm trying to figure out how to plot user growth over time. I figured the best way is to plot a cumulative sum. I put together a simple code:

我试图弄清楚如何绘制用户增长随时间的变化图。我认为最好的方法是绘制累积总和。我整理了一个简单的代码:

tmp = members[['Joined']].copy()
tmp['count'] = 1
tmp.set_index('Joined', inplace=True)

This produces the following cumsum:

这会产生以下结果cumsum

            count
Joined  
2017-08-19  1
2017-05-07  2
2017-11-29  3
2016-05-22  4
2017-01-23  5

Now when I try to plot this using tmp.plot()I get something super weird like this, uh:

现在,当我尝试使用绘制此图时,tmp.plot()我得到了这样的超级奇怪的东西,呃:

cumulative sum as plotted by pandas

Pandas绘制的累积总和

  1. I genuinely have no idea what is this plot actually displaying (this looks like some kind of cumulative delta trend line?)
  2. How do I plot cumulative user growth over time
  1. 我真的不知道这个图实际显示的是什么(这看起来像某种累积增量趋势线?)
  2. 我如何绘制随着时间的推移累积用户增长

The version of pandas I'm using: pandas (0.20.3)

我正在使用的Pandas版本: pandas (0.20.3)

In case you are curious whether the length of the series is the same as the highest count:

如果您想知道系列的长度是否与最高计数相同:

tmp.cumsum().max() == len(tmp)

count  True
dtype: bool

回答by YOBEN_S

Seems like you need sort_index, then cumsum, then plot

好像你需要sort_index,然后cumsum,然后plot

#tmp.index=pd.to_datetime(tmp.index)

tmp.sort_index().cumsum().plot()

enter image description here

在此处输入图片说明