8

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()

enter image description here

This is what i want (only show 4 but this is the gist): enter image description here

1 Answer 1

24

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

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

bar plot

Sign up to request clarification or add additional context in comments.

2 Comments

How can i rotate the x axis values?
@jxn Look at the docs for DataFrame.plot(). Passing rot=0 should do. If you are still in trouble ask another question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.