pandas 如何在同一图形上绘制两个 DataFrame 以进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31258134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to plot two DataFrame on same graph for comparison
提问by Zythyr
I have two DataFrames (trail1 and trail2) with the following columns: Genre, City, and Number Sold. Now I want to create a bar graph of both data sets for a side by side comparison of Genre vs. total Number Sold. For each genre, I want to two bars: one representing trail 1 and the other representing trail 2.
我有两个 DataFrames(trail1 和 trail2),包含以下列:Genre、City 和 Number Sold。现在我想创建两个数据集的条形图,以便并排比较流派与总销售数量。对于每个流派,我想要两个条形:一个代表路径 1,另一个代表路径 2。
How can I achieve this using Pandas?
如何使用 Pandas 实现这一目标?
I tried the following approach which did NOT work.
我尝试了以下不起作用的方法。
gf1 = df1.groupby(['Genre'])
gf2 = df2.groupby(['Genre'])
gf1Plot = gf1.sum().unstack().plot(kind='bar, stacked=False)
gf2Plot = gf2.sum().unstack().plot(kind='bar, ax=gf1Plot, stacked=False)
I want to be able to see How trail1 data set compared to trial2 data for each of the Genre (ex: Spicy, Sweet, Sour, etc...)
我希望能够看到 trail1 数据集与每个流派的 trial2 数据相比如何(例如:辣、甜、酸等......)
I also tried using concat, but I can't figure out how to graph the concatenated DataFrame on the same graph to compare the two keys.
我也尝试过使用 concat,但我不知道如何在同一图上绘制连接的 DataFrame 以比较两个键。
DF = pd.concat([df1,df2],keys=['trail1','trail2'])
回答by Zythyr
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: 
这是生成的图形的示例: 
回答by maxymoo
You're one the right track, but you want mergerather than concat. Try this:
你是一个正确的轨道,但你想要merge而不是concat。尝试这个:
DF = pd.merge(df1,df2,on=['Genre','City'])
DF.Groupby([['Genre','City']]).sum().unstack().plot(kind = 'bar')

