0

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?

1
  • df['cpu_system'] = df['cpu_system'].astype(float) Commented Jan 26, 2018 at 14:56

1 Answer 1

1

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()]
Sign up to request clarification or add additional context in comments.

1 Comment

Getting an error when running the first two lines: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: to_numeric() got an unexpected keyword argument 'erors'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.