2014-06-06 123 views
1

考慮兩個輸入文件b.datc.dat繪製兩個pandas groupby對象的堆積條形圖?

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 

我可以導入這些和組到每月總計:

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]) 

接下來,我想繪製堆疊條形圖。但是,我的嘗試會導致單獨的圖形。

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

有沒有人知道如何做到這一點?

回答

1

你可以通過使用concat來創建一個新的數據框並繪製它,但我認爲你必須重命名其中一列。

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