I've managed to plot subplots from a groupby. I have two columns 'A', and 'B', which I want to plot on subplot (1 per value in 'B') with their respective averages. I prepare my data by counting, dropping the duplicates, and then summing it up (if there is a more elegant way to do it, please let me know!).
df = pd.DataFrame([[1, 'cat1'], [1, 'cat1'], [4, 'cat2'], [3, 'cat1'], [5, 'cat1'],[1, 'cat2']], columns=['A', 'B'])
df = df[['A','B']]
df['count'] = df.groupby(['A','B'])['A'].transform('count')
df = df.drop_duplicates(['A','B'])
df = df.groupby(['A','B']).sum()
Then I unstack it and plot it with subplots:
plot = df.unstack().plot(kind='bar',subplots=True, sharex=True, sharey=True, layout = (3,3), legend=False)
plt.show(block=True)
I would like to add the mean for each category, but I have don't know: 1. How to calculate the mean. If I calculate it on the unstacked groupby, I get the mean of the count, rather than the value 'A'. 2. Once I have the mean value, I don't know how to plot it on the same subplot.
Any help is welcomed :)
--
Edit following Nickil Maveli's answer:
What I'm trying to achieve is to plot bars of the grouped values on A, and to plot a vertical line with the mean value on B. So using the graphs from Nickil Maveli, this would be:
From what I've found on stackexchange, I think I should be using plt.axvline(mean, color='r', linestyle='--')
. However, I don't know how to call have a different mean per plot.