【问题标题】:Python: Pandas: Matplotlib: Year over Year (x = month, y = donations for each year)Python:Pandas:Matplotlib:年复一年(x = 月,y = 每年的捐款)
【发布时间】:2021-05-13 16:16:23
【问题描述】:

我有一个简单的数据集,其中一列 = 日期 (01/31/2014...01/31/2021),另一列 = 值 (45000, 45500, 50000...)

date value
01/31/2014 450000

我可以通过使用 x = 月,y = 每年的捐赠来旋转数据来生成这个:

但我真的只想在今年 - 3(2021、2020、2019、2018)这样做。

我花了一段时间搜索但无法弄清楚。感谢您的帮助。

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    你可以尝试使用like

    df[df['date'] >= '01/01/2018']
    

    【讨论】:

    • 这不起作用,因为'01/01/2018' 不是日期时间格式。
    • 哈哈你说得对,我的错,我忘了调整。
    【解决方案2】:

    您可以通过绘制数据透视表来做到这一点。

    pv = pd.pivot_table(df.loc[df.date >= '2018-01-01'], index=df.date.dt.month, columns=df.date.dt.year, values='value', aggfunc='sum')
    pv.plot()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-05
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      相关资源
      最近更新 更多