Data Cleaning Basics.12

Hi!
Can someone explain me why the code in 1) works while the code in 2) doesn’t work ?
Shouldn’t they do the same things ?

1)

laptops["weight"] = laptops["weight"].str.replace("kgs","").str.replace("kg","").astype(float)
laptops.rename({"weight": "weight_kg"}, axis=1, inplace=True)
laptops.to_csv("laptops_cleaned.csv",index=False)

2)

laptops["weight"] = laptops["weight"].str.replace("kg","")
laptops["weight"] = laptops["weight"].str.replace("kgs","")
laptops["weight"] = laptops["weight"].astype(float)
laptops.rename({"weight":"weight_kg"},axis=1,inplace=True)
laptops.to_csv("laptops_cleaned.csv",index=False)

Sorry , I understand it now
I have to replace kgs before kg because otherwise it would remain a letter s and it’s impossible to make a cast to float.

2 Likes