Data Analysis with Python Projects - Medical Data Visualizer: ValueError catplot

Hello everyone!
I currently encounter the following error when I try to run the draw_cat_plot function in the gitpod workspace:

Traceback (most recent call last):
File “/workspace/boilerplate-medical-data-visualizer/medical_data_visualizer.py”, line 79, in
draw_cat_plot()
File “/workspace/boilerplate-medical-data-visualizer/medical_data_visualizer.py”, line 41, in draw_cat_plot
fig = sns.catplot(data=df_cat, x=“variable”, y=“total”, hue=“value”, kind=“bar”, col=‘cardio’)
File “/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/seaborn/categorical.py”, line 2782, in catplot
p = Plotter(
File “/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/seaborn/categorical.py”, line 67, in init
super().init(data=data, variables=variables)
File “/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/seaborn/_base.py”, line 634, in init
self.assign_variables(data, variables)
File “/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/seaborn/_base.py”, line 679, in assign_variables
plot_data = PlotData(data, variables)
File “/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/seaborn/_core/data.py”, line 58, in init
frame, names, ids = self._assign_variables(data, variables)
File “/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/seaborn/_core/data.py”, line 232, in _assign_variables
raise ValueError(err)
ValueError: Could not interpret value total for y. An entry with this name does not appear in data.

The strange thing about this is, that I don’t get this error when I run the code with my local python interpreter. Additionally the plot I get, when I run the code locally seems to be correct.
Here is the code of my draw_cat_plot function:

I would really appreciate help on this issue.

Can you share your code in not a screenshot? You can copy and paste it here. Otherwise someone would have to type this all out to test it.

What’s strange is that the line in the error does not match the line in your screenshot:

line 41, in draw_cat_plot
fig = sns.catplot(data=df_cat, x=“variable”, y=“total”, hue=“value”, kind=“bar”, col=‘cardio’)

Thank you for your response.
Here is the code copy pasted:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

#1
df = pd.read_csv('medical_examination.csv')
#2
df['overweight'] = ((df['weight'] / (df['height'] / 100)**2) > 25) * 1
#3
#Set good values to 0, "bad" values to 1
df['cholesterol'] = df['cholesterol'].replace(1, 0)
df['cholesterol'] = df['cholesterol'].replace(2, 1)
df['cholesterol'] = df['cholesterol'].replace(3, 1)

df['gluc'] = df['gluc'].replace(1, 0)
df['gluc'] = df['gluc'].replace(2, 1)
df['gluc'] = df['gluc'].replace(3, 1)

#4
def draw_cat_plot():
    # 5
    df_cat = pd.melt(df, id_vars='cardio', value_vars=['cholesterol', 'gluc', 'smoke', 'alco', 'active', 'overweight'])



    # 6
    grouped = df_cat.groupby('cardio')
    grouped_counts = grouped.value_counts()
    df_cat = pd.DataFrame(grouped_counts) #.rename(columns={'count': 'total'})
    df_cat = df_cat.sort_values(['cardio', 'variable'])
    

    # 7



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


    # 9
    fig.savefig('catplot.png')
    return fig

if __name__ == '__main__':
    draw_cat_plot()

The line in the error might not match because I deleted some empty lines for the screenshot. Here is the current error:

Traceback (most recent call last):
File “/workspace/boilerplate-medical-data-visualizer/medical_data_visualizer.py”, line 47, in
draw_cat_plot()
File “/workspace/boilerplate-medical-data-visualizer/medical_data_visualizer.py”, line 39, in draw_cat_plot
fig = sns.catplot(data=df_cat, x=“variable”, y=“count”, hue=“value”, kind=“bar”, col=‘cardio’)
File “/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/seaborn/categorical.py”, line 2782, in catplot
p = Plotter(
File “/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/seaborn/categorical.py”, line 67, in init
super().init(data=data, variables=variables)
File “/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/seaborn/_base.py”, line 634, in init
self.assign_variables(data, variables)
File “/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/seaborn/_base.py”, line 679, in assign_variables
plot_data = PlotData(data, variables)
File “/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/seaborn/_core/data.py”, line 58, in init
frame, names, ids = self._assign_variables(data, variables)
File “/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/seaborn/_core/data.py”, line 232, in _assign_variables
raise ValueError(err)
ValueError: Could not interpret value count for y. An entry with this name does not appear in data.

Can’t replicate your error, sorry.

Can you print the df_cat right before your fig = sns line and show the output?

Can you also link to the instructions so I can review?

EDIT: If you look at the example chart, the y-axis is named “total” so the tests might be looking for this. I think you were correct in the first place to rename that column “total”. Just make sure the column and the column you pass to sns as the y-axis have the same name.

It seems like the gitpod workspace has some issues right now. It says ‘Extension activation failed’ and therefore I cannot execute the code in the workspace right now. But this is the result of the print statement when I run it locally:

This is the link to the instructions :
https://www.freecodecamp.org/learn/data-analysis-with-python/data-analysis-with-python-projects/medical-data-visualizer

Ok, so the column is called total again, that’s good.

Did you update your y-axis in the sns function? Please share your current code and error.

You can also use replit and import this github repo https://github.com/freeCodeCamp/boilerplate-medical-data-visualizer/ and paste your code back into that