I have a Pandas DataFrame with item details. One of the columns is Weight and some values are stored as 200kgs, 120kgs etc in the Weight column.
I want to strip the 'kgs' string so that I can use the values for some calculation. I tried doing the same through a For loop to strip the 'kgs'
item = pd.read_csv('item_data.csv')
for x in item.Weight: # item.Weight shows the weights of the items
if type(x) == str:
item.Weight = x.strip('kgs')
else:
item.Weight = x
The above code strips the 'kgs' but displays the first value for all the rows!
item.Weight = [x.strip('kgs') if type(x)==str else x for x in item.Weight]
However, when i do list comprehension, as shown above, it works! Can you please explain why the For loop does not seem to work but the List Comprehension with the same logic works