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:

a = [['a', 0.1], ['b', ' 0.2'], ['c', '0.2,0.3']]
df = pd.DataFrame(a, columns= ['alphabet', 'floats'])


>>> df
  alphabet   floats
0        a      0.1
1        b      0.2
2        c  0.2,0.3

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:

>>> df
  alphabet   floats
0        a      0.1
1        b      0.2

Advertisement

Answer

use to_numeric()+dropna():

df=df.assign(floats=pd.to_numeric(df['floats'],errors='coerce')).dropna(subset=['floats'])

OR in 2 steps:

df['floats']=pd.to_numeric(df['floats'],errors='coerce')
df=df.dropna(subset=['floats'])

output:

  alphabet   floats
0        a      0.1
1        b      0.2
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement