2

I have a dataframe where a few indexes are string values. How can I delete the rows of data where the index is a string value or a non integer? Thanks for your help.

1 Answer 1

4

I would suggest using boolean indexing:

mask = frame.index.map(lambda x: not isinstance(x, str))
frame = frame[mask]
Sign up to request clarification or add additional context in comments.

2 Comments

I believe that before I set the index for the dataframe, the data type of that series is a string. So that when I run the boolean indexing, all of the dataframe is cleared. I try to change the datatype of the series before setting the index but I get the following error: invalid literal for int() with base 10 . Any suggestions?
@scriptdiddy: To avoid the error, replace int(x) with the expression int(text) if x.isdigit() else x.

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.