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
I would suggest using boolean indexing:
mask = frame.index.map(lambda x: not isinstance(x, str))
frame = frame[mask]
2 Comments
scriptdiddy
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?
unutbu
@scriptdiddy: To avoid the error, replace
int(x) with the expression int(text) if x.isdigit() else x.