Python Forum

Full Version: ValueError: could not convert string to float
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to convert an entire column (actually, three) to float, but there's something getting wrong.

This is the code:
series = df['time']
mydata = df[['points','lat','lon','t_mn']].apply(lambda x: x.str.replace(',', '.').astype(float), axis=1)
mydata2 = df[['lat','lon','t_mn']].apply(lambda x: x.str.replace(',', '.').astype(float), axis=1)
The Traceback I get is this:
Error:
Traceback (most recent call last): <ipython-input-39-f787414b7182> in <module>() if copy or is_object_dtype(arr) or is_object_dtype(dtype): # Explicit copy, or required since NumPy can't view from / to object. return arr.astype(dtype, copy=True) return arr.view(dtype) ValueError: could not convert string to float:
Output:
<class 'pandas.core.frame.DataFrame'> RangeIndex: 79 entries, 0 to 78 Data columns (total 5 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 points 79 non-null int64 1 time 79 non-null int64 2 lat 79 non-null object 3 lon 79 non-null object 4 t_mn 79 non-null object dtypes: int64(2), object(3) memory usage: 3.2+ KB
I already tried to do step by step, replacing the commas first and then trying to change to float, but I am still unsuccessful. I don't know if there is an error in my data, but it looks normal.