[英]pandas - groupby multiple columns and get unique count of other column
我有大约 20 列的数据框。 但是,在这种情况下,我关注 3 个特定的列。
(日期,小时,cust_id)
现在我想绘制如下图:-
1) 'x-axis' 将显示从 00 到 23 的小时数。
2) 'y-axis' 将代表该小时(唯一客户 ID)客户的计数。
3) 图表中的每一行将代表绘制它的特定日期。
我尝试了以下:-
fig, ax = plt.subplots()
for label, df1 in df.groupby('date'):
df1.groupby('hour').agg(customers = ('customer_id', 'nunique')).plot(ax=ax, label=label)
但是,上述问题很少:-
1) 每个折线图的标签都显示为“customer_id”,这应该是日期。
2)这张图是否正确?
尝试使用以下方法:
fig, ax = plt.subplots()
for label, df1 in df.groupby('date'):
ax.plot(df1.groupby('hour').agg(customers = ('customer_id', 'nunique')), label=label)
ax.legend()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.