Home > OS >  bar plotting sum of expenses for age group from to column
bar plotting sum of expenses for age group from to column

Time:08-27

I have a column for people in different age groups and their expenses. I want to plot how much each age group has spent

attempt 1

i grouped age group column and expenses like this:

temp=df.groupby('AGE')['EXP'].sum()

and i tried to plot the sum of expenses for all age groups this is what I tried

temp3.sum().unstack(0).plot.barh()

this did not work as I got this error

AttributeError: 'numpy.float64' object has no attribute 'plot'

attempt 2

here i just tried to plot it as x and y but numbers where not correct they were far off. i got values from 0-400 while true values for were 100-10000

fig, ax = plt.subplots(figsize=(6, 7))
sns.barplot(x='AGE',y=df['EXP'].values, ci=0, ax=ax,data=df)

CodePudding user response:

Your first approach is almost made it, I used count, not sum, because I count survey participants, but the idea is the same

(df.
 groupby(['age_group', 'budget'])['Score']
 .count()
 .unstack()
).plot.bar(width=0.7)

result bar chart:

enter image description here

You can reach the same result with crosstab, normalize will allow you directly switch to %:

pd.crosstab(df_filtered.age_group, df["Roughly how much do you spend on alcohol in a week?"], normalize='index').round(2).plot.bar(width=0.7)

plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))

enter image description here

  • Related