Medical Data Visualizer problem with values in bar graph

Hi everyone,
I’m currently doing the Data Analysis course and I have a problem with the Medical Data Visualizer, when I have to draw the cat plot.
I should be getting something like this:

but instead I get this:

All the values are the same. I think it’s a problem from df_cat but I don’t know why.
Here’s the code I used:

df_cat = pd.melt(df,id_vars=["cardio"], value_vars=["active","alco","cholesterol","gluc","overweight","smoke"])

df_cat = df_cat.groupby(["cardio","variable","value"]).size().to_frame("total").reset_index()

fig = sns.catplot(x="variable",y="total",data=df_cat,hue="value", kind="bar", col="cardio")

I dropped your posted code in my working version and the correct graph was generated (your code was substantially the same as mine anyway). So, the problem is likely elsewhere. Post a link to your project on repl.it or similar and may the issue can be found.

Hey!
Here you have a link to my repl:

https://replit.com/@IonutMarianMar1/boilerplate-medical-data-visualizer#main.py

I was working with PyCharm, because I like to run the code from time to time to see if it works, and when I uploaded the code the “normalize” part didn’t work on repl (don’t know why). But I changed that part and it seems like it still doesn’t work… Maybe the problem is there?

The problem is here:

df.loc[df["gluc"] == 1] = 0
df.loc[df["gluc"] != 0] = 1
df.loc[df["cholesterol"] == 1] = 0
df.loc[df["cholesterol"] != 0] = 1
df[df["overweight"]<=25] = 0
df[df["overweight"]>25] = 1

If you add a print(df) before and after the first line, you’ll see that this code is setting your data to zeroes as it matches. By the end, the entire dataframe is all zeroes.

As usual with pandas, there are numerous ways to make these kinds of changes.

It works! Thank you!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.