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?

2
  • 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) Commented Apr 1, 2021 at 19:19
  • so how can I drop string values from this column? Commented Apr 1, 2021 at 19:21

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.