如何在同一个图上绘制两个DataFrame进行比较 [英] How to plot two DataFrame on same graph for comparison
问题描述
我有两个数据框(trail1和trail2),包含以下列:流派,城市和已售数量。现在我想创建一个两个数据集的条形图,用于并行比较流派和总售数。对于每个类型,我想要两个酒吧:一个代表路径1,另一个代表路径2.
我如何使用熊猫来实现这个目标?
我尝试了下面的方法,但是没有奏效。
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)
我希望能够看到如何使用trail1数据集(例如:辣,甜,酸等)的试用2数据...
我也尝试过使用concat,但我无法确定了解如何在同一图形上绘制连接的DataFrame以比较两个键。
DF = pd.concat([df1,df2],keys = ['trail1','trail2'])
我找到了解决方案。我欢迎其他人发布更好的方法。
解决方案:
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')
生成的图的一个例子:
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.
How can I achieve this using 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...)
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.
DF = pd.concat([df1,df2],keys=['trail1','trail2'])
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:
这篇关于如何在同一个图上绘制两个DataFrame进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!