熊猫绘制计数器累计总和

时间:2018-02-12 04:01:33

标签: python pandas matplotlib plot

我有以下数据框:

    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

我试图弄清楚如何随着时间的推移绘制用户增长情况。我认为最好的方法是绘制累积总和。我把一个简单的代码放在一起:

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

这会产生以下cumsum

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

现在,当我尝试使用tmp.plot()绘制这个时,我得到了一些非常奇怪的东西,呃:

cumulative sum as plotted by pandas

  1. 我真的不知道这个情节实际显示的是什么(这看起来像某种累积的三角洲趋势线?)
  2. 如何绘制累积用户随时间的增长情况
  3. 我使用的大熊猫版本:pandas (0.20.3)

    如果您好奇系列的长度是否与最高计数相同:

    tmp.cumsum().max() == len(tmp)
    
    count  True
    dtype: bool
    

1 个答案:

答案 0 :(得分:3)

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

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

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

enter image description here

相关问题