6114617
6114617

Reputation: 79

Plotting Pivot Tables in Python

I have a dataframe that looks something like this:

airline     review
United      neutral
United      neutral
United      negative
Southwest   negative
Delta       positive
Delta       positive

I then converted it into a pivot table:

a = pd.pivot_table(df, index = ['airline', 'review'], aggfunc = len)
a
airline    review
United     neutral     2
United     negative    1
Southwest  negative    1
Delta      positive    2

Then plotted:

a.plot(kind = 'bar')

enter image description here

How can I get each airlines reviews to plot together as different colors for each type of review, all sorted together (much like a pivot chart would in Excel)?

Thanks in advance!

Upvotes: 1

Views: 5686

Answers (1)

akuiper
akuiper

Reputation: 214927

You just need to unstack the reviews index as columns:

a.unstack('review').plot(kind='bar')

enter image description here

Upvotes: 1

Related Questions