Lina Linutina
Lina Linutina

Reputation: 443

How to convert a string to a float in my dataframe? (Python)

I'm working with my dataframe in Python. Dataframe looks like this:

Timestamp                  cpu_system           Host
 2018-01-09 20:03:22     1.3240749835968018     pwp2
 2017-09-30 21:03:22     2.0                    pwp2 
 ...................................................   

When I check on dtypes on this dataframe, I get this:

   timestamp     object
   cpu_system    object
   host          object
 dtype: object

I want to change cpu_system into float. Running this code:

 df[['cpu_system']] = df[['cpu_system']].astype(float)

Getting this error:

ValueError: could not convert string to float: value

How should I fix it?

Upvotes: 0

Views: 65

Answers (1)

jezrael
jezrael

Reputation: 862641

You can first check what values cannot be converted by:

print (df[pd.to_numeric(df['cpu_system'], errors='coerce').isnull()])

and then use to_numeric with parameter erors='coerce' for convert bad values to NaN:

df['cpu_system'] = pd.to_numeric(df['cpu_system'], errors='coerce')

And filter problematic values out if necessary by boolean indexing:

df = df[df['cpu_system'].notnull()]

Upvotes: 1

Related Questions