【问题标题】:Pandas plot cumulative sum of counters over time熊猫随着时间的推移绘制计数器的累积总和
【发布时间】:2018-02-12 04:01:33
【问题描述】:

我有以下数据框:

    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() 进行绘制时,我得到了一些超级奇怪的东西,呃:

  1. 我真的不知道这个图实际显示的是什么(这看起来像是某种累积 delta 趋势线?)
  2. 如何绘制随时间的累积用户增长 ????

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

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

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

count  True
dtype: bool

【问题讨论】:

    标签: python pandas matplotlib plot


    【解决方案1】:

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

    #tmp.index=pd.to_datetime(tmp.index)
    
    tmp.sort_index().cumsum().plot()
    

    【讨论】:

    • 嗨@YOBEN_S。这个答案真的很有帮助。如果 datetime 列不是索引,您将如何绘制?
    • @SOK 您可以将其设置为 index ,例如 df.set_index('date')['col1').cumsum().plot()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多