pandas 将数据框绘制为多个条形图 [英] pandas plot dataframe as multiple bar charts

查看:80
本文介绍了 pandas 将数据框绘制为多个条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的熊猫数据框df:

          ab             channel   booked
0    control             book_it     466
1    control          contact_me     536
2    control             instant     17
3  treatment             book_it     494
4  treatment          contact_me     56
5  treatment             instant     22

我想绘制3组条形图(根据channel):

每个频道的

: 地块控制预定值与治疗预定值.

因此,我应该在3个组中得到6个条形图,其中每个组都有控制和治疗预定值.

到目前为止,我只能预定,但不能按ab分组:

ax = df_conv['booked'].plot(kind='bar',figsize=(15,10), fontsize=12)
ax.set_xlabel('dim_contact_channel',fontsize=12)
ax.set_ylabel('channel',fontsize=12)
plt.show()

这是我想要的(仅显示4,但这是要点):

解决方案

旋转数据框,使控制值和处理值在单独的列中.

df.pivot(index='channel', columns='ab', values='booked').plot(kind='bar')

This is my pandas dataframe df:

          ab             channel   booked
0    control             book_it     466
1    control          contact_me     536
2    control             instant     17
3  treatment             book_it     494
4  treatment          contact_me     56
5  treatment             instant     22

I want to plot 3 groups of bar chart (according to channel):

for each channel: plot control booked value vs treatment booked value.

hence i should get 6 bar charts, in 3 groups where each group has control and treatment booked values.

SO far i was only able to plot booked but not grouped by ab:

ax = df_conv['booked'].plot(kind='bar',figsize=(15,10), fontsize=12)
ax.set_xlabel('dim_contact_channel',fontsize=12)
ax.set_ylabel('channel',fontsize=12)
plt.show()

This is what i want (only show 4 but this is the gist):

解决方案

Pivot the dataframe so control and treatment values are in separate columns.

df.pivot(index='channel', columns='ab', values='booked').plot(kind='bar')

这篇关于 pandas 将数据框绘制为多个条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆