12. Challenge: Clean a String Column -- Actual different from Expected

def conv_col(i):
    i = i.replace('kgs','').replace('kg','')
    return i

new_weight = []
for w in laptops['weight']:
    new_w = conv_col(w)
laptops['weight'] = new_weight
laptops = laptops.astype({'weight':float})
laptops = laptops.rename({'weight':'weight_kg'},axis=1)

laptops.to_csv('laptops_cleaned.csv', index=False)

When i ran my code, i did,
print(laptops[‘weight’].value_counts(dropna=False)) before my code &
print(laptops[‘weight_kg’].value_counts(dropna=False)) after my code to verify and total number of rows in the output aren’t matching. I’d greatly appreciate if anyone can help me figure this out. Thanks!

Hi @morbaleankur

Welcome to the community. Could you please refer these guidelines ?
It will be easier to understand your question if you follow the guidelines and format the code.

1 Like


my thoughts:

  • If you were able to run laptops[‘weight’] = new_weight without an error, I am assuming both lengths matched?
  • Instead of running print(laptops[‘weight’].value_counts() what happens when you run print(len(laptops[‘weight’]) before and compare to print(len(laptops[‘weight_kg’]) after?
1 Like

Hi Ivan @semenchuk.ivan , when i ran print(len(laptops[‘weight’]) before and print(len(laptops[‘weight_kg’]) after, i got the same results. Does this mean my solution is correct??

Thank you @jithins123. Will do the needful.

Yes, your solution is accepted as correct answer.
If your list new_weight was not the same length as laptops['weight'], you would get an error on this line:

laptops[‘weight’] = new_weight

and the error would say:

ValueError: Length of values does not match length of index

1 Like

Oh, I see. I greatly appreciate your feedback. Thank you Ivan!

1 Like