【发布时间】:2026-01-30 16:15:01
【问题描述】:
假设使用的数据是这样的
df = pd.DataFrame({'Order_id': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
'Order_date': ['10/1/2020', '10/1/2020', '11/1/2020', '11/1/2020', '12/1/2020', '12/1/2020', '12/1/2020', '12/1/2020', '13/1/2020', '13/1/2020'],
'Product_nr': [0, 2, 1, 0, 2, 0, 2, 1, 2, 0],
'Quantity': [3, 1, 6, 5, 10, 1, 2, 5, 4, 3]})
#transforming the date column into datetime
df['Order_date'] = pd.to_datetime(df['Order_date'])
我正在尝试绘制给定时间跨度内每种产品每天订购的产品数量。
我最初的想法是这样的
product_groups = df.groupby(['Product_nr'])
products_daily = pd.DataFrame()
for product, total_orders in product_groups:
products_daily[product.day] = total_orders.values
products_daily.plot(subplots=True, legend=False)
pyplot.show()
我知道必须有一个groupby('Product_nr'),并且应该使用Grouper(freq='D') 将日期分成几天。它们也应该是一个 for 循环来组合它们,然后将它们全部绘制出来,但我真的不知道如何将这些部分组合在一起。我怎样才能归档这个?我的最终目标实际上是每月为每个产品绘制超过 4 年的销售记录,但鉴于此处的示例数据,我将其更改为每日。
也欢迎任何关于指南、教程的建议或链接。非常感谢!
【问题讨论】:
-
您是否要为每个产品生成一个图?
-
@QuangHoang 是的,因为我认为随着时间的推移更容易看出订单是否有任何模式
-
@TrentonMcKinney 哦,对不起,这是我第一次在这里发帖,所以我不知道。我已经编辑了我的帖子并在那里放了一些示例数据。谢谢你告诉我。
标签: python pandas matplotlib data-visualization data-science