Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas drop rows based on a column's data type

Tags:

python

pandas

I'd like to drop the rows that is not a float type in one column, how to do that? Thanks in advance.

         a     b    c 
    1   1.2   2.2   3.3
    2   rvtg  1.2   2.2
    3   2.2   2.2   1.6 

as cell "a2" is rvtg, not a float, so i'd like to drop row 2, I tried several code but it gives error msg, could anyone pls help? thanks.

like image 330
Zeshen Zhang Avatar asked Dec 23 '22 00:12

Zeshen Zhang


1 Answers

Apply pd.to_numeric and drop NaNs. Please note that this is a more generic solution. If you know which column is expected to have non-numeric values, you can avoid using apply.

df.apply(pd.to_numeric, errors = 'coerce').dropna()

    a   b   c
1   1.2 2.2 3.3
3   2.2 2.2 1.6

Edit: Since only column a is likely to contain non-numeric values, a more efficient solution would be to convert column a to numeric rather than applying the function to all the columns. The following will return same output.

df['a'] = pd.to_numeric(df['a'], errors = 'coerce')
df.dropna(inplace = True)
like image 113
Vaishali Avatar answered Jan 06 '23 20:01

Vaishali