Reputation:
total_income_language = pd.DataFrame(df.groupby('language')['gross'].sum())
average_income_language = pd.DataFrame(df.groupby('language')['gross'].mean())
plt.bar(total_income_language.index, total_income_language["gross"],
label="Total Income of Language")
plt.bar(average_income_language.index, average_income_language["gross"],
label="Average Income of Language")
plt.xlabel("Language")
plt.ylabel("Log Dollar Values(Gross)")
I want to plot the sum and average for each and every languages. I'm not sure if my code does what I wanted. And I'm getting an error while trying to plot this. I'm not sure where did I messed up on the coding. I need some assistance.
Upvotes: 2
Views: 282
Reputation: 11105
Instead of:
df.groupby('language')['gross'].sum()
Try this:
df.groupby('language').sum()
And similarly with mean()
. That should get your code closer to running.
Calling the groupby()
method of a DataFrame yields a groupby
object upon which you then need to call an aggregation function, like sum
, mean
, or agg
. The groupby
documentation is really great: https://pandas.pydata.org/pandas-docs/stable/groupby.html
Also, you might be able to achieve your desired output in two lines:
df.groupby('language').sum().plot(kind='bar')
df.groupby('language').mean().plot(kind='bar')
Upvotes: 1
Reputation: 862701
You can use groupby
with aggregation by agg
, rename columns by dict
and plot by DataFrame.plot.bar
.
Last set labels by ax.set
.
df = pd.DataFrame({'language':['en','de','en','de','sk','sk'],
'gross':[10,20,30,40,50,60]})
print (df)
gross language
0 10 en
1 20 de
2 30 en
3 40 de
4 50 sk
5 60 sk
d = {'mean':'Average Income of Language','sum':'Total Income of Language'}
df1 = df.groupby('language')['gross'].agg(['sum','mean']).rename(columns=d)
print (df1)
Total Income of Language Average Income of Language
language
de 60 30
en 40 20
sk 110 55
ax = df1.plot.bar()
ax.set(xlabel='Language', ylabel='Log Dollar Values(Gross)')
If want rotate labels of axis x
:
ax = df1.plot.bar(rot=0)
ax.set(xlabel='Language', ylabel='Log Dollar Values(Gross)')
Upvotes: 2