简体   繁体   中英

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?

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()]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM