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'
CodePudding user response:
Use df.index.isin
, not df.index.str.isin
:
df = df.loc[df.index.isin(name_list)]
CodePudding user response:
You can just do reindex
df = df.reindex(name_list)