Skip to content
Advertisement

index string has no method of isin()

I have a dataframe with index is string name like ‘apple’ etc.

Now I have a list

name_list=[‘apple’,’orange’,’tomato’]

I’d like to filter dataframe rows by selecting rows with index is in the above list

df=df.loc[df.index.str.isin(name_list)]

then I got an error of

AttributeError: 'StringMethods' object has no attribute 'isin'

Advertisement

Answer

Use df.index.isin, not df.index.str.isin:

df = df.loc[df.index.isin(name_list)]
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement