Value error in Python: Could not convert string to float: Male

Hi All,

I hope this message finds you well. I have encountered an error and hoping is someone could assist me in getting this resolved. I attempted to make a countplot with the Gender attribute however, when executing the code, I have received, ValueError: could not convert string to float: ‘Male’ as per image below. Would someone please help?

Thank you in advance.

FYI please copy and paste the text, no screen shots please.
You can use the preformatted text button </> to then format the text for easy reading. The screen shots are harder to read and imposible to copy text from.

```
print("Just like this")
```

Why are you using such an old version of python? 3.7 stopped being supported a lonbg time ago.

It appears that your data source has the string ‘Male’ in a column that you are expecting to contain a number. That’s not a Python problem, it’s either a data quality problem or a misunderstanding of the data schema.

It’s expecting you to pass it a summary of how many people there are of each gender, but you’re passing it a single column that just states the gender of each person.

1 Like

Dear Barry Scott,

Thank you for your message. This code and Python instructions were conveyed by a tutorial online which is why the version seems to be used. If there is a more profound version please could you kindly suggest?

FYI, thank you for your recommendation regarding the formatting of the code for questions. I am a beginner in Python so relying on all the supportive comments from the community. FYI please see the below for more information regarding the code and thank you for your reply.

Categorical attributes visualisation

sns.countplot(df[‘Gender’])

The below refers to the traceback

ValueError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_4912\704947111.py in
1 # Exploratory Data Analysis
2 # Categorical attributes visualisation
----> 3 sns.countplot(df[‘Gender’])

c:\users\tabhr\appdata\local\programs\python\python37\lib\site-packages\seaborn\categorical.py in countplot(data, x, y, hue, order, hue_order, orient, color, palette, saturation, width, dodge, ax, **kwargs)
2945 estimator, errorbar, n_boot, units, seed,
2946 orient, color, palette, saturation,
→ 2947 width, errcolor, errwidth, capsize, dodge
2948 )
2949

c:\users\tabhr\appdata\local\programs\python\python37\lib\site-packages\seaborn\categorical.py in init(self, x, y, hue, data, order, hue_order, estimator, errorbar, n_boot, units, seed, orient, color, palette, saturation, width, errcolor, errwidth, capsize, dodge)
1529 “”“Initialize the plotter.”“”
1530 self.establish_variables(x, y, hue, data, orient,
→ 1531 order, hue_order, units)
1532 self.establish_colors(color, palette, saturation)
1533 self.estimate_statistic(estimator, errorbar, n_boot, seed)

c:\users\tabhr\appdata\local\programs\python\python37\lib\site-packages\seaborn\categorical.py in establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
514
515 # Convert to a list of arrays, the common representation
→ 516 plot_data = [np.asarray(d, float) for d in plot_data]
517
518 # The group names will just be numeric indices

c:\users\tabhr\appdata\local\programs\python\python37\lib\site-packages\seaborn\categorical.py in (.0)
514
515 # Convert to a list of arrays, the common representation
→ 516 plot_data = [np.asarray(d, float) for d in plot_data]
517
518 # The group names will just be numeric indices

c:\users\tabhr\appdata\local\programs\python\python37\lib\site-packages\pandas\core\series.py in array(self, dtype)
855 dtype=‘datetime64[ns]’)
856 “”"
→ 857 return np.asarray(self._values, dtype)
858
859 # ----------------------------------------------------------------------

ValueError: could not convert string to float: ‘Male’

Please do let me know if you require any more information. Thanks

Dear Matthew,

Thank you for your reply and explaining the issue. Would you please suggest the correct code for a summary for each gender?

Thank you

Dear Kevin,

Thank you for your reply. The data and code has come from an online tutorial. Seems like an issue with the tutorial. Thank you for your help!