【问题标题】:Plot Pandas timeseries cumulative number of unique occurences in column over time [closed]随着时间的推移,绘制 Pandas 时间序列列中唯一出现的累计次数[关闭]
【发布时间】:2021-02-03 04:30:00
【问题描述】:

我有一个示例数据集,例如:

   Datetime    value
   1.10.2020   x 
   1.10.2020   y
   2.10.2020   x 
   3.10.2020   z
   3.10.2020   x
   3.10.2020   y
   4.10.2020   x
   4.10.2020   y
   5.10.2020   x
   5.10.2020   z

我想随着时间的推移绘制每个唯一值在列值中出现的次数的累积总和。在这种情况下,图中将有三条线,标签 x、y、z。 Y 轴有累计出现次数(例如 x = 5),x 轴有日期时间列。

【问题讨论】:

    标签: python pandas datetime matplotlib


    【解决方案1】:

    您可以使用 cumcount 计算 value 中值的频率。将索引设置为Datetime 并绘制分组值

    df['cumcount'] = df.groupby('value').cumcount() + 1
    df.set_index(pd.to_datetime(df.Datetime)).groupby('value')['cumcount'].plot(style='x-')
    plt.legend();
    

    输出:

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多