Plot a stacked bar graph from two pandas groupby objects?

atomh33ls

Consider two input files b.dat and c.dat:

string,date,number
a string,2/5/11 9:16am,1.0
a string,3/5/11 10:44pm,2.0
a string,4/22/11 12:07pm,3.0
a string,4/22/11 12:10pm,4.0
a string,4/29/11 11:59am,1.0
a string,5/2/11 1:41pm,2.0
a string,5/2/11 2:02pm,3.0
a string,5/2/11 2:56pm,4.0
a string,5/2/11 3:00pm,5.0
a string,5/2/14 3:02pm,6.0
a string,5/2/14 3:18pm,7.0


string,date,number
a string,2/5/10 9:16am,4.0
a string,3/4/10 10:44pm,5.0
a string,4/22/10 12:07pm,3.0
a string,6/22/10 12:10pm,6.0
a string,4/29/11 11:59am,1.0
a string,5/2/11 1:41pm,9.0
a string,5/27/11 2:02pm,3.0
a string,6/2/11 2:56pm,14.0
a string,5/2/11 3:00pm,5.0
a string,5/8/14 3:02pm,16.0
a string,5/2/14 3:18pm,7.0

I can import these and group into monthly totals:

b=pd.read_csv('b.dat')
c=pd.read_csv('c.dat')
b.index=b['date']
c.index=c['date']
b['date']=pd.to_datetime(b['date'],format='%m/%d/%y %I:%M%p')
c['date']=pd.to_datetime(c['date'],format='%m/%d/%y %I:%M%p')
bg=pd.groupby(b,by=[b.index.year,b.index.month])
cg=pd.groupby(c,by=[c.index.year,c.index.month])

Next I'd like to plot a stacked bar graph. However my attempts result in separate graphs.

bg.sum().plot(kind='bar',stacked=True)
cg.sum().plot(kind='bar',stacked=True)

Does anyone know how this can be done?

Ajean

You can do it by using concat to make a new dataframe and plotting that, though I think you'll have to rename one of the columns.

cgs = cg.sum()
cgs.columns = ['number2']
d = pd.concat([bg.sum(), cgs], axis=1)
d.plot(kind='bar', stacked=True)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related