How to create a plotly histogram chart between BMI and mean of charges for that BMI instead of count

I am trying to create a chart by BMI and respective mean of charges. In the lecture, the histogram is plotted in a way that BMI is on the x axis and the count is on the y axis. When I try to get charges on Y axis, I am getting sum of charges. I am trying to get mean of charges instead. Can you please help?

y_ax=medical_df.charges.groupby(medical_df[‘bmi’]).mean()
fig1 = px.histogram(medical_df,
x=‘bmi’,
y=y_ax,
marginal=‘box’)
fig1.update_layout(bargap=0.1)
fig1.show()

Hey, welcome to the forum.
If I am not wrong you want to get the mean of charges per BMI correct? I think your groupby statement is wrong.
Can you try the code below?

y_ax = medical_df.groupby('bmi')['charges'].mean()

Hope this solves your problem. If not please reply to this thread.

Hi Birajde! Thank you for replying. What I am trying to do is create a histogram chart where on the x axis I have BMI and on Y axis the mean of charges for that BMI. If I just use x=‘bmi’ and y=‘charges’ then it gives sum of charges. Do you know how can I get the mean of charges plotted on y axis respective to BMI on x axis?

Okay, so I am not sure about histogram cause it plots the count of a range/category in a graph. I think line plot might be a better option here. maybe you can use the code given above and plot y_ax.index in x axis and y_ax.values in y-axis in a line plot.