AttributeError: 'str' object has no attribute 'get'

‘’’

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

# Import data
df =pd.read_csv('medical_examination.csv')

# Add 'overweight' column
df['overweight'] = df["weight"]/((df["height"])/100*(df["height"])/100)
df.loc[df["overweight"]>25, "overweight"]=1
df.loc[df["overweight"]<=25, "overweight"]=0
# Normalize data by making 0 always good and 1 always bad. If the value of 'cholesterol' or 'gluc' is 1, make the value 0. If the value is more than 1, make the value 1.
df.loc[df["overweight"]>25, "overweight"]=1
df.loc[df["overweight"]<=25, "overweight"]=0

# Draw Categorical Plot
def draw_cat_plot():
    # Create DataFrame for cat plot using `pd.melt` using just the values from 'cholesterol', 'gluc', 'smoke', 'alco', 'active', and 'overweight'.
    df_cat = pd.melt(df, id_vars = ["cardio"], value_vars = ["cholesterol", "gluc", "alco", "active", "smoke", "overweight"])


    # Group and reformat the data to split it by 'cardio'. Show the counts of each feature. You will have to rename one of the columns for the catplot to work correctly.
    df_cat = df_cat.value_counts().reset_index(name="total")

    # Draw the catplot with 'sns.catplot()'
    var = ["active", "alco", "cholesterol", "gluc", "overweight", "smoke"]
    fig = sns.catplot(x ="variable", y= "total", hue ="value", data = "df_cat", kind = "bar", order = var, col = "cardio").fig



    # Do not modify the next two lines
    fig.savefig('catplot.png')
    return fig


# Draw Heat Map
def draw_heat_map():
    # Clean the data
    df_heat = df[(df["ap_lo"]<=df["ap_hi"])&(df["height"]>=df["height"].quantile(0.025))&(df["height"]<=df["height"].quantile(0.0975))&(df["weight"]>=df["weight"].quantile(0.025))&(df["weight"]<=df["weight"].quantile(0.0975))]

‘’’
Output
‘’’

python main.py
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-ztkaysjt because the default path (/config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    import medical_data_visualizer
  File "/home/runner/boilerplate-medical-data-visualizer-3/medical_data_visualizer.py", line 7, in <module>
    df =pd.read_csv('medical_data_visualizer.csv')
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/util/_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/parsers/readers.py", line 586, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/parsers/readers.py", line 482, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/parsers/readers.py", line 811, in __init__
    self._engine = self._make_engine(self.engine)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/parsers/readers.py", line 1040, in _make_engine
    return mapping[engine](self.f, **self.options)  # type: ignore[call-arg]
 python main.py
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-1t3e315s because the default path (/config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
Traceback (most recent call last):
  File "main.py", line 6, in <module>
    medical_data_visualizer.draw_cat_plot()
  File "/home/runner/boilerplate-medical-data-visualizer-3/medical_data_visualizer.py", line 28, in draw_cat_plot
    fig = sns.catplot(x ="variable", y= "total", hue ="value", data = "df_cat", kind = "bar", order = var, col = "cardio").fig
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/seaborn/_decorators.py", line 46, in inner_f
    return f(**kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/seaborn/categorical.py", line 3792, in catplot
    p.establish_variables(x_, y_, hue, data, orient, order, hue_order)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/seaborn/categorical.py", line 144, in establish_variables
    x = data.get(x, x)
AttributeError: 'str' object has no attribute 'get'

‘’’

"df_cat" is a string, you want df_cat.

2 Likes

thanks, that settled it

But what about this
‘’’

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

# Import data
df =pd.read_csv('medical_examination.csv')

# Add 'overweight' column
df['overweight'] = df["weight"]/((df["height"])/100*(df["height"])/100)
df.loc[df["overweight"]>25, "overweight"]=1
df.loc[df["overweight"]<=25, "overweight"]=0
# Normalize data by making 0 always good and 1 always bad. If the value of 'cholesterol' or 'gluc' is 1, make the value 0. If the value is more than 1, make the value 1.
df.loc[df["overweight"]>25, "overweight"]=1
df.loc[df["overweight"]<=25, "overweight"]=0

# Draw Categorical Plot
def draw_cat_plot():
    # Create DataFrame for cat plot using `pd.melt` using just the values from 'cholesterol', 'gluc', 'smoke', 'alco', 'active', and 'overweight'.
    df_cat = pd.melt(df, id_vars = ["cardio"], value_vars = ["cholesterol", "gluc", "alco", "active", "smoke", "overweight"])


    # Group and reformat the data to split it by 'cardio'. Show the counts of each feature. You will have to rename one of the columns for the catplot to work correctly.
    df_cat = df_cat.value_counts().reset_index(name="total")

    # Draw the catplot with 'sns.catplot()'
    var = ["active", "alco", "cholesterol", "gluc", "overweight", "smoke"]
    fig = sns.catplot(x ="variable", y= "total", hue ="value", data = df_cat, kind = "bar", order = var, col = "cardio").fig



    # Do not modify the next two lines
    fig.savefig('catplot.png')
    return fig


# Draw Heat Map
def draw_heat_map():
    # Clean the data
    df_heat = df[(df["ap_lo"]<=df["ap_hi"])&(df["height"]>=df["height"].quantile(0.025))&(df["height"]<=df["height"].quantile(0.0975))&(df["weight"]>=df["weight"].quantile(0.025))&(df["weight"]<=df["weight"].quantile(0.0975))]

    # Calculate the correlation matrix
    corr = df_heat.corr()

    # Generate a mask for the upper triangle
    mask = np.triu(np.ones_like(corr))



    # Set up the matplotlib figure
    fig, ax = plt.subplots(figsize = (12,9))

    # Draw the heatmap with 'sns.heatmap()'
    ax = sns.heatmap(corr, mask = mask, vmax =.4, square = True)



    # Do not modify the next two lines
    fig.savefig('heatmap.png')
    return fig

‘’’

its outputting two failures, but i cant figure out how to fix it.

‘’’

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

# Import data
df =pd.read_csv('medical_examination.csv')

# Add 'overweight' column
df['overweight'] = df["weight"]/((df["height"])/100*(df["height"])/100)
df.loc[df["overweight"]>25, "overweight"]=1
df.loc[df["overweight"]<=25, "overweight"]=0
# Normalize data by making 0 always good and 1 always bad. If the value of 'cholesterol' or 'gluc' is 1, make the value 0. If the value is more than 1, make the value 1.
df.loc[df["overweight"]>25, "overweight"]=1
df.loc[df["overweight"]<=25, "overweight"]=0

# Draw Categorical Plot
def draw_cat_plot():
    # Create DataFrame for cat plot using `pd.melt` using just the values from 'cholesterol', 'gluc', 'smoke', 'alco', 'active', and 'overweight'.
    df_cat = pd.melt(df, id_vars = ["cardio"], value_vars = ["cholesterol", "gluc", "alco", "active", "smoke", "overweight"])


    # Group and reformat the data to split it by 'cardio'. Show the counts of each feature. You will have to rename one of the columns for the catplot to work correctly.
    df_cat = df_cat.value_counts().reset_index(name="total")

    # Draw the catplot with 'sns.catplot()'
    var = ["active", "alco", "cholesterol", "gluc", "overweight", "smoke"]
    fig = sns.catplot(x ="variable", y= "total", hue ="value", data = df_cat, kind = "bar", order = var, col = "cardio").fig



    # Do not modify the next two lines
    fig.savefig('catplot.png')
    return fig


# Draw Heat Map
def draw_heat_map():
    # Clean the data
    df_heat = df[(df["ap_lo"]<=df["ap_hi"])&(df["height"]>=df["height"].quantile(0.025))&(df["height"]<=df["height"].quantile(0.0975))&(df["weight"]>=df["weight"].quantile(0.025))&(df["weight"]<=df["weight"].quantile(0.0975))]

    # Calculate the correlation matrix
    corr = df_heat.corr()

    # Generate a mask for the upper triangle
    mask = np.triu(np.ones_like(corr))



    # Set up the matplotlib figure
    fig, ax = plt.subplots(figsize = (12,9))

    # Draw the heatmap with 'sns.heatmap()'
    ax = sns.heatmap(corr, mask = mask, vmax =.4, square = True)



    # Do not modify the next two lines
    fig.savefig('heatmap.png')
    return fig

‘’’

The formatting of your post is hella weird - how about you just include a link to your replit?

As for the failures, it’d be nice to know the error-messages.

For the catplot, you need to add a line fig=fig.fig before saving - as the test is not designed for standard object sns is returning.

As for the heatmap - without error message no idea what might be wrong.

1 Like

Am sorry for my formatting errors Jagaya, am new to this . I also have trouble successfuly posting questions on stack overflow due to my poor formatting skills. You can put me through if possible.

Here is the link to my boiler account:

Thanks for your assistance.

‘’’

 python main.py
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-8ch2izlw because the default path (/config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
F..[]
F
======================================================================
FAIL: test_bar_plot_number_of_bars (test_module.CatPlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-medical-data-visualizer-3/test_module.py", line 28, in test_bar_plot_number_of_bars
    self.assertEqual(actual, expected, "Expected a different number of bars chart.")
AssertionError: 25 != 13 : Expected a different number of bars chart.

======================================================================
FAIL: test_heat_map_values (test_module.HeatMapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-medical-data-visualizer-3/test_module.py", line 47, in test_heat_map_values
    self.assertEqual(actual, expected, "Expected different values in heat map.")
AssertionError: Lists differ: [] != ['0.0', '0.0', '-0.0', '0.0', '-0.1', '0.5[616 chars]0.1']

Second list contains 91 additional elements.
First extra element 0:
'0.0'

Diff is 941 characters long. Set self.maxDiff to None to see it. : Expected different values in heat map.

----------------------------------------------------------------------
Ran 4 tests in 6.647s

FAILED (failures=2)
Above is the error message, and below is  the link to my account
#https://replit.com/@Dandave11/boilerplate-medical-data-visualizer-3#medical_data_visualizer.py

‘’’

Yeah formatting can be tricky.

Anyway, the error messages are very helpful.
First please look at the project files, you are actually provided with example-files which show you how an solution can look like - this way you can compare to your images and spot differences.

Your barchart is having more bars than it should have - please read the task again and look at your image, which bars shouldn’t be there (it looks like you didn’t normalize the data).

Your heatmap is lacking annotations. Please refer to the seaborn documentation about heatmaps and it should tell you, how to add those.

1 Like

Thank you so much, i have handled the issue of normalizing the data. But what i cant figure out now is the heatmap and the annotations stuff…please any more hint.

i tried working on the antonnation and got this
‘’’

Matplotlib created a temporary config/cache directory at /tmp/matplotlib-7wkwepa8 because the default path (/config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
...['0.0', '0.0', '0.0', '-0.0', '-0.0', '-0.0', '-0.0', '-0.0', '-0.1', '0.1', '0.0', '0.3', '0.0', '-0.0', '0.1', '0.0', '0.2', '0.0', '0.0', '0.1', '0.7', '0.0', '0.1', '-0.0', '-0.0', '0.0', '0.1', '0.1', '-0.0', '0.0', '-0.0', '0.0', '0.0', '0.1', '0.0', '0.3', '0.0', '0.0', '0.3', '-0.0', '-0.1', '0.0', '0.0', '0.0', '0.0', '0.0', '0.1', '0.2', '-0.0', '-0.0', '0.1', '0.1', '0.1', '0.0', '0.3', '0.0', '-0.0', '-0.0', '0.0', '0.0', '-0.0', '0.0', '-0.0', '0.0', '0.1', '-0.0', '0.0', '0.3', '-0.1', '-0.1', '0.0', '0.4', '0.3', '0.2', '0.1', '0.0', '0.0', '-0.0']
F
======================================================================
FAIL: test_heat_map_values (test_module.HeatMapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-medical-data-visualizer-3/test_module.py", line 47, in test_heat_map_
 python main.py
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-2ab5glp7 because the default path (/config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
...['0.0', '0.0', '0.0', '-0.0', '-0.0', '-0.0', '-0.0', '-0.0', '-0.1', '0.1', '0.0', '0.3', '0.0', '-0.0', '0.1', '0.0', '0.2', '0.0', '0.0', '0.1', '0.7', '0.0', '0.1', '-0.0', '-0.0', '0.0', '0.1', '0.1', '-0.0', '0.0', '-0.0', '0.0', '0.0', '0.1', '0.0', '0.3', '0.0', '0.0', '0.3', '-0.0', '-0.1', '0.0', '0.0', '0.0', '0.0', '0.0', '0.1', '0.2', '-0.0', '-0.0', '0.1', '0.1', '0.1', '0.0', '0.3', '0.0', '-0.0', '-0.0', '0.0', '0.0', '-0.0', '0.0', '-0.0', '0.0', '0.1', '-0.0', '0.0', '0.3', '-0.1', '-0.1', '0.0', '0.4', '0.3', '0.2', '0.1', '0.0', '0.0', '-0.0']
F
======================================================================
FAIL: test_heat_map_values (test_module.HeatMapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-medical-data-visualizer-3/test_module.py", line 47, in test_heat_map_values
    self.assertEqual(actual, expected, "Expected different values in heat map.")
AssertionError: Lists differ: ['0.0', '0.0', '0.0', '-0.0', '-0.0', '-0.0', '-0.0', '-0[507 chars]0.0'] != ['0.0', '0.0', '-0.0', '0.0', '-0.1', '0.5', '0.0', '0.1'[601 chars]0.1']

First differing element 2:
'0.0'
'-0.0'

Second list contains 13 additional elements.
First extra element 78:
'-0.0'

Diff is 1537 characters long. Set self.maxDiff to None to see it. : Expected different values in heat map.

----------------------------------------------------------------------
Ran 4 tests in 7.538s

FAILED (failures=1)


‘’’

3https://replit.com/@Dandave11/boilerplate-medical-data-visualizer-3#medical_data_visualizer.py

can u help me figure out what i still need to do

Looking at your heatmap and the provided example - your “overweight” column is broken. I think the way you clean it would turn ALL entries to 0.

And you are calculating the wrong quantiles.
If 0.025 represents 2.5%, then 0.0975 cannot be 97.5%

1 Like

honestly speaking, i dont know what else to do. I have corrected the percentile to 0.975.
This is the way i cleaned the overweight column:

df.loc[df["overweight"]>25, "overweight"]=1

df.loc[df["overweight"]<=25, "overweight"]=0

But i dont know what else to do.

The problem is, you turn all values above 25 to 1. Then you turn all values below 25 (which includes 1) to 0 → so you’ve turned all values to 0.

There are a couple of other options. You can create a temporary column, use df as reference for df_heat, use a couple of transformer-methods (.map, .apply, maybe more) or just think about how to write those two statements so they don’t interfere with eachother.

1 Like

Thanks so much Jagaya. I used the lambda funtion, and it solved the problem. Am so grateful.

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

2 Likes

Thank you so much, I think I have learnt how to properly insert backticks(```).

Good to hear and well done :wink:
As I said, there are several other options and once you work with larger datasets, it’s worth exploring them, as some are significantly faster than others. But there is always more to learn, so don’t forget to celebrate your achievements inbetween.

1 Like

yeah, thanks alot Jagaya. You have been kind and very helpful.

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