How to plot two DataFrame on same graph for comparison

筅森魡賤 提交于 2019-12-03 08:04:44

I found a solution to my question. I welcome others to post a better approach.

Solution:

df1 = pd.DataFrame(myData1, columns=['Genre', 'City', 'Sold'])
df2 = pd.DataFrame(myData2, columns=['Genre', 'City', 'Sold'])

df1['Key'] = 'trail1'
df2['Key'] = 'trail2'

DF = pd.concat([df1,df2],keys=['trail1','trail2'])

DFGroup = DF.groupby(['Genre','Key'])

DFGPlot = DFGroup.sum().unstack('Key').plot(kind='bar')

Here is an example of the generated graph:

You're one the right track, but you want merge rather than concat. Try this:

DF = pd.merge(df1,df2,on=['Genre','City'])
DF.Groupby([['Genre','City']]).sum().unstack().plot(kind = 'bar')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!