Skip to content Skip to sidebar Skip to footer

Column In Data Frame With Some Numbers In Double Quotes; Trying To Change To Float

So I would like to use astype method to change my df column from string to float. but some of the numbers are are in double quotes like so: ''38000.00'' I would like to iterate th

Solution 1:

You can also use strip() for this. I don't know what your pd.DataFrame() looks like so I just created a simple one here.

s = pd.Series(['"39000.00"', '"38000.00"', '"37000.00"'])
data = pd.DataFrame(s, columns=['TOT_VAL'])
lst = data['TOT_VAL'].tolist()

lst_strip = [i.strip('"') for i in lst]
to_float = [float(num) for num in lst_strip]

data['TOT_VAL'] = to_float
data
TOT_VAL
039000138000237000data['TOT_VAL'].dtypes
dtype('float64')

Solution 2:

I think 'apply' will solve this in a much better way. df['TOT_VAL'].apply(lambda x : float(x.strip('"'))). I hope it can do a little help.

Post a Comment for "Column In Data Frame With Some Numbers In Double Quotes; Trying To Change To Float"