6

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'])

2 Answers 2

11

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

Sign up to request clarification or add additional context in comments.

Comments

-1

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')

1 Comment

This solution doesn't work for me. I need to groupby only Genre or City; not both. On my bar graph, I want to two bars for each genre: one bar for representing data set 1 and the other bar representing data set 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.