2

I have a pandas df and I want to remove non-numeric values of col1.

If I use df[df.col1.apply(lambda x: x.isnumeric())], I get the following error:

AttributeError: 'float' object has no attribute 'isnumeric'

any suggestion on doing this efficiently in pandas?

Phoenix
  • 359
  • 7
  • 15
  • 1
    strings have an `isnumeric()` attribute, but a float clearly doesn't need one because it _has_ to be numeric. You probably have an Object column where some values are strings, and others are missing and `NaN` is a float, so add some error handling like `if not pd.isnull(x)`. (But really you should do this with `pd.to_numeric` instead of Series.apply) – ALollz Apr 01 '21 at 19:19
  • so how can I drop string values from this column? – Phoenix Apr 01 '21 at 19:21

1 Answers1

1

You could use standard method of strings isnumeric and apply it to each value in your id column:
Remove non-numeric rows in one column with pandas
Python replace non digit character in a dataframe

Qwerty
  • 81
  • 4