My question is similar to this one.
I want to convert all the values in the dataframe to float type. But what is need more is to ignore the rows where such conversions cannot happen.
For example, given a string '0.9', it will be converted to float successfully but a string like 'why' will through an error. I want to remove all such rows in the dataframe which would come under the error case.
Try this:
df = df.apply(pd.to_numeric, errors='coerce')
From docs :
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
If 'raise' , then invalid parsing will raise an exception
If 'coerce' , then invalid parsing will be set as NaN
If 'ignore' , then invalid parsing will return the input
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.