考虑两个输入文件b.dat
和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
我可以导入这些并分组到每月总计中:
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)
有谁知道如何做到这一点?
答案 0 :(得分:1)
你可以通过使用concat创建一个新的数据框并绘制它,尽管我认为你必须重命名其中一个列。
cgs = cg.sum()
cgs.columns = ['number2']
d = pd.concat([bg.sum(), cgs], axis=1)
d.plot(kind='bar', stacked=True)