Issue
Following situation:
I have a dataframe (df) with the following scheme:
Gender | AgeGroup | |
---|---|---|
0 | m | Group1 |
1 | m | Group2 |
2 | f | Group2 |
3 | f | Group1 |
I want to do a stacked bar chart using pandas & matplotlib, with the age groups on the x axis and the amount of genders on a stacked bar on the y-axis.
I tried plotting it with various:
df.plot.bar(column=["AgeGroup", "Gender"], stacked=True)
But I always get the error that there are no numeric values inside the dataframe, which makes sense, but I am trying to count those.
What would the ideal solution be here?
Solution
Use a crosstab
:
pd.crosstab(df['AgeGroup'], df['Gender']).plot.bar(stacked=True)
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.