Skip to content
Advertisement

Pandas, how can cast a column as floats that hold floats, strings, and strings that cannot be converted?

Let’s say I have the following pandas df:

JavaScript

And I wish to remove all rows in which the value of the column floats cannot be cast as float and cast all values as float that can be cast as such:

JavaScript

Advertisement

Answer

use to_numeric()+dropna():

JavaScript

OR in 2 steps:

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement