Cleaning a string column using pandas

Screen Link:

My Code:

laptops[“weight”] = laptops[“weight”].str.replace(“kg”, “”).str.replace(“kgs”, “”).astype(float)

What I expected to happen:
I tired to remove “kg” and “kgs” from values in the “weight” column and to convert them into float data type.

What actually happened:
Then I got an error message like below:
ValueError: could not convert string to float: ‘4s’

Replace this line with the output/error
I changed the order of the code, so str.replace(“kgs”, “”) comes before str.replace(“kg”, “”), and I could run the program as expected.

I don’t understand why I have to remove “kgs” before “kg”…!

1 Like

Hi @woogyong34,

Let’s use “4kgs” as an example.

If you remove “kg” first, “4kgs” will become “4s”. And if you next remove “kgs”, nothing will be removed because “4s” doesn’t have “kgs”.

“4kgs” → “4s” → “4s”

The reverse is better because “4kgs” will remove “kgs” first and becomes “4”. Next, removing “kg” is also fine because “4” will still be “4”.

“4kgs” → “4” → “4”

Feel free to ask if anything is unclear.

1 Like