用Python绘制数据透视表

时间:2017-02-23 02:58:51

标签: python pandas plot pivot-table

我的数据框看起来像这样:

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

然后我将其转换为数据透视表:

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

然后绘制:

a.plot(kind = 'bar')

enter image description here

如何将每个航空公司的评论一起绘制为每种类型的评论的不同颜色,所有评论都排序在一起(很像Excel中的数据透视图)?

提前致谢!

1 个答案:

答案 0 :(得分:0)

您只需将reviews索引取消堆叠为列:

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

enter image description here

相关问题