Python Forum
ValueError: could not convert string to float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: could not convert string to float
#1
Sad 
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Indirectlty convert string to float in JSON file WBPYTHON 6 8,183 May-06-2020, 12:09 PM
Last Post: WBPYTHON
  ValueError: could not convert string to float RahulSingh 3 5,385 Apr-09-2020, 02:59 PM
Last Post: dinesh
  convert a list of string+bytes into a list of strings (python 3) pacscaloupsu 4 13,141 Mar-17-2020, 07:21 AM
Last Post: markfilan
  Convert dataframe string column to numeric in Python darpInd 1 2,934 Mar-14-2020, 10:07 AM
Last Post: ndc85430
  convert 'A B C' to numpy float matrix rezabma 4 3,655 Feb-27-2020, 09:48 AM
Last Post: rezabma
  ValueError: could not convert string to float: '4 AVENUE' Kudzo 4 7,434 Jan-26-2020, 10:47 PM
Last Post: Kudzo
  Convert 'object' to 'string' AdWill97 1 64,807 May-06-2019, 08:22 AM
Last Post: Yoriz
  ValueError: could not convert the string to float Grin 3 11,331 Jun-14-2018, 08:17 PM
Last Post: killerrex
  Problema with convert image to string karlo123 1 3,360 May-16-2018, 10:44 PM
Last Post: karlo123
  Error: ValueError: could not convert string to float: 'L200 1.6 D/C' Jaarroy 2 7,519 Jan-18-2018, 02:00 PM
Last Post: Jaarroy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020