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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With