Manz
Manz

Reputation: 605

ValueError: could not convert string to float Using Python

I have been looking to remove all the non numerical character, and convert the same to float type, But whenever any String character comes up , it gives the Error as "ValueError: could not convert string to float"

Please suggest how to resolve it .

Input File

col1           col2  

122.45         NaN
Ninety Five    3585/-
9987           178@#?
225 Nine       1983.86
Twelve         7363*

Output File

col1           col2  

122.45         NaN
NaN            3585
9987           178
225            1983.86
NaN            7363

Code i am using :

df[['col1','col2']] = df[['col1','col2']].replace('([^\d/.])', '', regex=True).astype(float)

Getting the Error:

ValueError: could not convert string to float

Upvotes: 0

Views: 2073

Answers (1)

Will Da Silva
Will Da Silva

Reputation: 7040

You need to use a raw string (with the r in front) for regex patterns, or double backslash (\\) escapes. Also you need \. to match literal . characters, not /.:

df[['col1', 'col2']] = df[['col1', 'col2']].replace('(-?[^\d\.])', '', regex=True).replace('', float('NaN')).astype(float)

Upvotes: 1

Related Questions