Dametime
Dametime

Reputation: 723

Dataframe horizontal stacked bar plot

I am reading the movielens user data. 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

I would like the plot to be similar to the example in http://benalexkeen.com/bar-charts-in-matplotlib/ The data format is like :

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

Upvotes: 0

Views: 66

Answers (1)

YOLO
YOLO

Reputation: 21749

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

enter image description here

Upvotes: 1

Related Questions