【问题标题】:Python Groupby and plotting of dataPython Groupby和数据绘图
【发布时间】:2022-01-22 22:53:26
【问题描述】:

您好,我有如下所示的数据框:

有没有办法绘制这个?例如,对于 2021 年 1 月 5 日,总计 10+7=17。图表需要显示每日统计数据,即 2021 年 1 月 5 日,总数为 17,温度为 36c。

对不起,我忘了添加..我想显示 btn 温度和总温度是否存在相关性 谢谢!

【问题讨论】:

  • 你可以简单地使用 df.groupby{'Date'].sum()["total"].plot(kind="bar")

标签: python pandas dataframe pandas-groupby


【解决方案1】:

你想要一个条形图吗?那么

编辑:感谢 not_speshal 指出温度列

(df.assign(temp=lambda x: x["Temperature"].str.extract("(\d+)").astype(float))
.groupby("Date").agg({"Total":"sum","temp":"first"})[["Total","temp"]].plot(kind="bar"))

【讨论】:

  • 感谢您的帮助!
【解决方案2】:

试试:

#convert date column to datetime
df["Date"] = pd.to_datetime(df["Date"], format="%d-%m-%Y")

#extract numbers from temperature column
df["Temperature"] = df["Temperature"].str.extract("(\d+)").astype(float)

#groupby to create the required plot data
df_plot = df.groupby("Date").agg({"Total": "sum", "Temperature": "first"})
df_plot.index = df_plot.index.date

#plot
>>> df_plot.plot.bar()

【讨论】:

  • 点赞!我错过了 op 也想要温度的事实
  • 谢谢@Yefet!不过,我们有非常相似的方法;)
  • 非常感谢!
【解决方案3】:

考虑到您正在使用 pandas,以下内容可以帮助您

pdf.groupby(['Date']).sum()[['Total']]

其中 pdf 是包含数据的 pandas 数据框。然后将其绘制成图表。

【讨论】:

  • Op 说他想要一个不显示数据框的图表
猜你喜欢
  • 2018-09-26
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多