How to plot two DataFrame on same graph for comparison

前端 未结 2 1489
陌清茗
陌清茗 2021-02-10 03:21

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 compari

2条回答
  •  有刺的猬
    2021-02-10 04:13

    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: enter image description here

提交回复
热议问题