简体   繁体   English

数据框水平堆积条形图

[英]Dataframe horizontal stacked bar plot

I am reading the movielens user data.我正在阅读 movielens 用户数据。 I want to plot the age and occupation grouped by gender (in two separate plots).我想绘制按性别分组的年龄和职业(在两个单独的图中)。 But I get this error:但我收到此错误:

user_df.groupby(['gender'])['age'].unstack().plot.bar()

AttributeError: Cannot access callable attribute 'unstack' of 'SeriesGroupBy' objects, try using the 'apply' method AttributeError: 无法访问“SeriesGroupBy”对象的可调用属性“unstack”,请尝试使用“apply”方法

I would like the plot to be similar to the example in http://benalexkeen.com/bar-charts-in-matplotlib/ The data format is like :我希望该图类似于http://benalexkeen.com/bar-charts-in-matplotlib/ 中的示例数据格式如下:

user_id age gender  occupation  zipcode
0   1   24  M   technician  85711
1   2   53  F   other   94043
2   3   23  M   writer  32067
3   4   24  M   technician  43537
4   5   33  F   other   15213

You can try something like this:你可以尝试这样的事情:

df.groupby(['occupation'])['user_id'].nunique().plot.bar()

For both gender and occupation , you can do:对于性别职业,您可以执行以下操作:

df.groupby(['occupation','gender'])['user_id'].size().unstack().plot.bar()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM