ValueError: could not convert string to float: '4s'

Screen Link:

My Code:


What I expected to happen:
My code is almost identical to the answer written for this challenge with the exception that I changed the name of the column first and tried to change the data type of the column.

What actually happened:
As code is identical to the answer, especially the second line where data type for the column weight is being converted, I expected it to convert smoothly to float from string.

ValueErrorTraceback (most recent call last)
<ipython-input-1-b47787f4c9ed> in <module>()
      1 laptops.rename({"weight":"weight_kg"},axis=1,inplace=True)
----> 2 laptops["weight_kg"]=laptops["weight_kg"].str.replace("kg","").str.replace("kgs","").astype(float)
      3 laptops.to_csv('laptops_cleaned.csv',index=False)
ValueError: could not convert string to float: '4s'


There is a major difference between the solution and your code. Here’s a simplified version of the issue.

>>> s="450kgs"
>>> s.replace("kg", "").replace("kgs", "")

Does the output surprise you? Think about why you wouldn’t be able to convert it to float.

Why is the output what it is? If you’re having trouble figuring it out, print s.replace("kg","") and see what it looks like.


Oops! I got it. As I was replacing “kg” first, so it replaced “kgs” with “s” and in the next iteration of replace method no replacement was made at all as no “kgs” was found in the series.

And then when I used astype(float), it could not convert string 4s to float.

Thanks. Got it all now.


I have been having the same issue and this post made me solve it. Thanks a lot.
At this stage in the Dataquest course, we did not learn regular expressions, I researched a bit while trying to solve this issue. For this particular problem, I think the pattern is quite simple to match and would avoid any mistakes such as the one we encountered.

laptops['weight'] = laptops.weight.str.replace(r'kgs?', '')

The question mark in the kgs? matching pattern basically says that the preceding character is optional. Therefore it will automatically match kg and kgs without having to use replace() twice.

Hope it helps.