Plotting two groupby() series in python
<p>I am new to python in general but I did do a lot of research to see if I can find a solution to this problem. Hope you guys can help.</p>

<p>Say A is a dataframe with Cost and site fields. B is a similar dataframe with cost and site fields. I want to group by the Site field and plot A/B for each site as a bar graph.</p>

<pre><code>A= pd.DataFrame({'Cost':[20,30,40,50,60,60,82,92,35], 
                 'Site':['S1','S1','S2','S3','S3','S3','S4','S5','S5']}) 
B= pd.DataFrame({'Cost':[40,75,92,105,110,200,15,62,32,12], 
                 'Site':['S1','S2','S2','S3','S4','S1','S5','S3','S4','S5']})
C=A.groupby('Site')['Cost'].sum()/B.groupby('Site')['Cost'].sum()
</code></pre>

<p>Now how do I plot a bar chart to plot C where each bar is a site name and the displayed value is from C?</p>

<p>You're nearly there!</p>

<pre><code>import pylab as P
t = P.bar(range(5),C,tick_label = C.index, align = 'center')
</code></pre>

<p>The first argument tells pylab there are 5 bars, the second gives the values from C, tick_label and alignment just name and align the bar labels.</p>

python dataframe