Plotting a grouped pandas data in plotly bar chart

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:


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 as px
import pandas as pd
df = pd.DataFrame()
df['country'] = ['India', 'India', 'Japan', 'Japan', 'China']
df['sold'] = [5, 3, 7, 2, 6]
fig =, x='country', y='sold') 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.index=[‘Japan’,‘Japan’,‘Korea’,‘India’,‘India’,‘USA’,‘USA’,‘USA’ ]

Hi @Yuechean , this can be used for plotting two grouped line charts.

fig.add_trace(go.Scatter(x=y1.index, y=y1,mode=‘lines’,name=‘Mean’))

You can also use line+bar combination or bar+bar or any other.

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

You can use add trace for other groupings as well like variance,mean,standard deviation etc on the same plot.