Remove rows where column value type is Pandas row

I have a pandas framework. One of my columns should only be floating. When I try to convert this column to float, I warn that there are rows. I would like to delete all rows where the values ​​in this column are rows ...

+6
source share
2 answers

Use convert_objects with the convert_numeric=True parameter, this will force any non-numeric values ​​to be NaN :

 In [24]: df = pd.DataFrame({'a': [0.1,0.5,'jasdh', 9.0]}) df Out[24]: a 0 0.1 1 0.5 2 jasdh 3 9 In [27]: df.convert_objects(convert_numeric=True) Out[27]: a 0 0.1 1 0.5 2 NaN 3 9.0 In [29]: 

Then you can delete them:

 df.convert_objects(convert_numeric=True).dropna() Out[29]: a 0 0.1 1 0.5 3 9.0 

UPDATE

Starting with version 0.17.0 this method is now deprecated , and you need to use to_numeric , unfortunately, this works on Series , and not on the whole df, so the equivalent code is now:

 df.apply(lambda x: pd.to_numeric(x, errors='coerce')).dropna() 
+10
source

The column data type can be found from the dtype.kind attribute. Something like df[col].dtype.kind . See numpy docs for more details. Transport a data block to go from indexes to columns.

0
source

Source: https://habr.com/ru/post/977805/


All Articles