I have a pandas dataframe which looks like this:
Country Sold
Japan 3432
Japan 4364
Korea 2231
India 1130
India 2342
USA 4333
USA 2356
USA 3423
I want to plot graphs using this dataframe. The plot will have country names on X-axis
and the mean/sum
of the sold of each country will on y-axis
. I know I can compute the mean/sum using the group by function like this:
df.groupby('Country')['Sold'].mean()
I know the plotly histogram has the function that can directly compute the value and plot the graph. But I want to apply on other graph of plotly
such as bar chart
to make the graph more interactive. I am having a hard time figuring it out. Can anyone please help me out?
Hi @Yuechean, if you want to plot the sum you can do it with plotly express:
import plotly.express as px
import pandas as pd
df = pd.DataFrame()
df['country'] = ['India', 'India', 'Japan', 'Japan', 'China']
df['sold'] = [5, 3, 7, 2, 6]
fig = px.bar(df, x='country', y='sold')
fig.show()
px.bar
will add one bar marker for each line, so that the different lines for a given country
will add up.
Hi @Emmanuelle, I also want to plot the mean, variance or standard deviation. Is there any way to do it?
df=pd.DataFrame([3432,4364,2231,1130,2342,4333,2356,3423])
df.index=[‘Japan’,‘Japan’,‘Korea’,‘India’,‘India’,‘USA’,‘USA’,‘USA’ ]
df.columns=[‘Sold’]
df.index.name=‘Country’
Hi @Yuechean , this can be used for plotting two grouped line charts.
x=df.groupby(‘Country’)[‘Sold’].sum()
y1=df.groupby(‘Country’)[‘Sold’].mean()
fig=px.line(x)
fig.add_trace(go.Scatter(x=y1.index, y=y1,mode=‘lines’,name=‘Mean’))
fig.show()
You can also use line+bar combination or bar+bar or any other.
x=df.groupby(‘Country’)[‘Sold’].sum()
y1=df.groupby(‘Country’)[‘Sold’].mean()
fig=px.line(x)
fig.add_trace(go.Bar(x=y1.index, y=y1,name=‘Mean’))
#This will plot Line for Sum and Bar for Mean in the same plot
fig.show()
You can use add trace for other groupings as well like variance,mean,standard deviation etc on the same plot.