Data Cleaning Basics Q.12

Why does the below code have different outcome from the standard answer? I feel like the logic behind them is the same.

My answer:

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

Standard answer:

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)

And I find the difference in the pic below(my values are all missing a ‘0’ compared with the standard one):

Hi @gaoweidong999

Have you tried identifying the data type for laptops["weight"] and/or laptops["weight_kg"] column(s).

Try that once and you would know why 1.9 is not equal to 1.90!

The only problem in your code is this line. You haven’t reassigned it back to the column!

Thank you so much! I assigned the value back and it worked!

But when I try to identify the data type for laptops["weight_kg"], it says dtype(‘0’). What does that mean and why 1.9 is not equal to 1.90?

image