HimanAB
HimanAB

Reputation: 2573

Plot the size of each group in a groupby object in Pandas

I have a dataframe and the following code gives me the number of transactions per gender for one of the two 'branches' 0 and 1. So imagine we have the following groupby object

data.groupby(['branch','gender']).agg(np.size)['count']
branch      gender
0           FEMALE    1317771
            MALE      1214686
1           FEMALE     254173
            MALE       179210
Name: count, dtype: int64

I did this because I wanted to create a bar plot that shows the number of transactions for each gender and branch together.

Here is how I did it: F and M denote female and male.

plt.bar([0,1,2,3,],data.groupby(['end_reason','gender']).agg(np.size)['listener_id'],align='center')
plt.xticks(range(4),['F_0','M_0','F_1','M_1'])
plt.title("Number of transactions per gender and branch")

The thing is I am sure this is not the right way to do it as I had to hardcode the labels myself. I think there might be a better way to do that because imagine we had more groups and it wasn't practical to hardcode all the group combinations in the xticks. Thanks

Upvotes: 2

Views: 1750

Answers (1)

Joe
Joe

Reputation: 12417

If I understood you correctly and your df is something like this:

   branch  gender  listener_id
0       0    male            1
1       0    male            3
2       1  female            2
3       1  female            4
4       1    male            1

You could do so:

import matplotlib
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')
df.groupby(['branch','gender']).agg(np.size)['listener_id'].unstack().plot(kind='bar')
plt.show()

and have: enter image description here

Upvotes: 1

Related Questions