Intermediate Python and Pandas/Data Cleaning Basics/string to float conversion

Screen Link:
Intermediate Python and Pandas/
data-cleaning-basics/12/challenge-clean-a-string-column
My Code: <laptops[“weight”].str.replace(“kg”,"").str.replace(“kgs”,"").astype(float)>
this one is not letting me change to float value…

Replace this line with your code

What I expected to happen:

What actually happened:

Replace this line with the output/error

The posted link points to a wrong page. Could you post the correct link?

Hello @mansi.trivedi, welcome to the community!

The error is in the order of your str.replace() methods:

The correct line of code is:
laptops["weight"].str.replace("kgs","").str.replace("kg","").astype(float)

Note that: str.replace("kgs","") comes before str.replace("kg","") in this line of code, this way the kgs substring is first replaced in the string before the kg substring is replaced.

I hope this helps.

1 Like