Lukasz
Lukasz

Reputation: 2606

Pandas bar graph using original dataframe

I have a pandas dataframe and I'm attempting to plot the number of subscription types purchased by gender the original dataframe resemblems

df = 
Memb_ID   Gender   1_Month   3_Month   6_Month   1_Year
1         Male     6         0         3         0
2         Male     0         0         0         4
3         Female   0         4         3         1
4         Male     2         1         0         1
5         Female   1         4         0         2
...

At the moment I make a temp_df where I sum up the data so that I have

temp_df = pd.DataFrame(columns=['Gender', '1_Year', '1_Month', '3_Month','6_Month'])

sex = ['Male', 'Female']
temp_df['Gender'] = sex

for i in list(temp_df.columns.values)[1:]:
    temp = [df.loc[df['Gender'] == 'Male', i].sum(),\
            df.loc[df['Gender'] == 'Female', i].sum()]
    temp_df[i] = temp

temp_df.plot(x='Gender', kind='bar', grid=True)
plt.show()

This fills up temp_df and I'm able to graph it. Is there an eloquent way of performing the same thing using just df?

Upvotes: 1

Views: 1487

Answers (1)

akuiper
akuiper

Reputation: 214957

You can use groupby().sum() to replace the temp_df:

ax = df.groupby('Gender')['1_Year','1_Month','3_Month','6_Month'].sum().plot.bar(grid=True)

enter image description here

Upvotes: 4

Related Questions