How to plot two DataFrame on same graph for comparison

2024/9/19 8:52:15

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

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

https://en.xdnf.cn/q/72227.html

Related Q&A

Babel doesnt recognize jinja2 extraction method for language support

Im adding language translation support to my project. The code is on Python and has jinja2 in the html files, and Javascript.Im trying to use Babel to do the translation, but it doesnt recognize the ex…

Automatically simplifying/refactoring Python code (e.g. for loops - list comprehension)? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Knowing the number of iterations needed for convergence in SVR scikit-learn

I am trying to optimize an SVR model and facing a problem because of overfitting, to overcome this I have tried to decrease the number of iterations instead of leaving it until convergence.To compare …

Why is `NaN` considered smaller than `-np.inf` in numpy?

What is the reason that NaNs are considered less than -np.inf in any comparisons involving np.min or np.argmin?import numpy as np In [73]: m = np.array([np.nan, 1., 0., -np.inf]) In [74]: n = np.array…

recursion within a class

I am trying to place a recursive formula inside a class statementclass SomeNode:def __init__(self, a):leng = len(a)half= leng/2self.firstnode=a[0][0]self.child1=SomeNode([a[i]for k in range(leng)])self…

There is an example of Spyne client?

Im trying to use spyne (http://spyne.io) in my server with ZeroMQ and MsgPack. Ive followed the examples to program the server side, but i cant find any example that helps me to know how to program the…

Safely bind method from one class to another class in Python [duplicate]

This question already has answers here:What is the difference between a function, an unbound method and a bound method?(6 answers)Closed 5 years ago.I know I can attach a function to a class and make …

Basic Python OpenCV cropping and resizing

can someone help me with a little cropping algorithm? its openCV.. im trying to figure this out. I know the method is crop = image[y:y1, x:x1]. If I have an image with new_dimensionXxnew_dimensionY pi…

Why does Keras loss drop dramatically after the first epoch?

Im training a U-Net CNN in Keras/Tensorflow and find that loss massively decreases between the last batch of the first epoch, and the first batch of the second epoch: Epoch 00001: loss improved from in…

extract strings from a binary file in python

I have a project where I am given a file and i need to extract the strings from the file. Basically think of the "strings" command in linux but im doing this in python. The next condition is …