使用单词列表从数据框中定位行

Locating rows from dataframe with list of words

如何使用单词列表作为参考从数据框中 select 行?

例如我有一个数据框 df_business,最后一列中的每个项目都是一个包含逗号分隔类别的字符串,如下所示:

categories: "Restaurants, Burgers, Coffee & Tea, Fast Food, Food"

我试过了,但它只给出了在其类别中仅包含 coffee 这个词的企业的行:

bus_int = df_business.loc[(df_business['categories'].isin(['Coffee']))]

如何获得包含我的字词的企业,即使它出现在其他人中,如上所示?

你要的是contains方法:

bus_int = df_business.loc[df_business.categories.str.contains('Coffee', regex=False)]

(默认情况下将提供的参数视为正则表达式,如果您只是像这样查找子字符串,则不需要。)

只需使用-.isin(List_name/the你想要的字符串)

例子-

list = ['apple', 'mango', 'banana'] #creating a list

df[df['Fruits_name'].isin(list)] 
# this will find all the rows with this 3 fruit name