在Python中绘制groupby()

发布于 2025-02-04 12:52:21 字数 1013 浏览 2 评论 0原文

我找不到一种从follwoing数据框架中绘制分组数据的方法:

                Processed Card       Transaction ID  Transaction amount     Error_Occured  
Date                                                                   
2019-01-01           Carte Rouge    217142203412           147924.21       0
2019-01-01              ChinaPay    149207925233            65301.63       1 
2019-01-01            Masterkard    766507067450           487356.91       5
2019-01-01                  VIZA    145484139636            97774.52       1
2019-01-02           Carte Rouge    510466748547           320951.10       3

我想创建一个图: x轴:日期,y轴:errors_occured,点/线,由处理的卡颜色 。我尝试先对数据框进行分组,并使用Pandas图绘制它:

df = df.groupby(['Date','Processed Card']).sum('Error_Occured')
df = df.reset_index()
df.set_index("Date",inplace=True)
df.plot(legend=True)
plt.show()

但是我得到了显示事务ID而不是卡片的图: 请参阅图

I cannot find a way to plot the grouped data from the follwoing data frame:

                Processed Card       Transaction ID  Transaction amount     Error_Occured  
Date                                                                   
2019-01-01           Carte Rouge    217142203412           147924.21       0
2019-01-01              ChinaPay    149207925233            65301.63       1 
2019-01-01            Masterkard    766507067450           487356.91       5
2019-01-01                  VIZA    145484139636            97774.52       1
2019-01-02           Carte Rouge    510466748547           320951.10       3

I want to create a plot where: x-axis: Date, y-axis: Errors_Occured, points/lines colored by Processed Card. I tried grouping the data frame first and ploting it using pandas plot:

df = df.groupby(['Date','Processed Card']).sum('Error_Occured')
df = df.reset_index()
df.set_index("Date",inplace=True)
df.plot(legend=True)
plt.show()

But I get the plot where Transaction ID is displayed and not the cards:
SEE THE PLOT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唐婉 2025-02-11 12:52:21

您可以做类似的事情:

fig, ax = plt.subplots()

df = pd.DataFrame()
df['Date'] = ['2019-01-01','2019-01-01','2019-01-01','2019-01-01','2019-01-02']
df['Card'] = ['Carte Rouge', 'ChinaPay', 'Masterkard', 'VIZA', 'Carte Rouge']
df['Error_Occured'] = [0,1,5,1,3]

series = dict(list(df.groupby(['Card'])))

for name, s in series.items():
    ax.plot(s['Date'], s['Error_Occured'], marker='o', label=name)
    
plt.legend()
plt.show()

这将与提供的数据产生以下内容:

“在此处输入图像说明”

请注意,您只想按卡而不是日期分组。

You can do something like this:

fig, ax = plt.subplots()

df = pd.DataFrame()
df['Date'] = ['2019-01-01','2019-01-01','2019-01-01','2019-01-01','2019-01-02']
df['Card'] = ['Carte Rouge', 'ChinaPay', 'Masterkard', 'VIZA', 'Carte Rouge']
df['Error_Occured'] = [0,1,5,1,3]

series = dict(list(df.groupby(['Card'])))

for name, s in series.items():
    ax.plot(s['Date'], s['Error_Occured'], marker='o', label=name)
    
plt.legend()
plt.show()

This produces the following with the data provided:

enter image description here

Note that you only want to group by card, not date.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文