如何检查行是否包含单词列表中的某个字符串

时间:2017-05-12 23:54:59

标签: python regex string pandas

for i in range(len(ingName)):
    x = ingName[i]
    #check if the string contains IngName ignoring uppercase Sub_Seg_Sub_Cat
    df = df_grocery[df_grocery['Sub_Seg_Sub_Cat'].str.contains(str(ingName[i]), case=False)]
    df['IngredientName'] = ingName[i]
    df['IngredientID'] = ingID[i]

    #write it out to a csv
    df.to_csv("ingred11.csv",  mode = 'a', encoding='utf-8', header=None)

检查列中的行是否包含某些单词的最佳方法是什么?我正在使用str.contains,它正在获取子字符串。我想要它检查意大利面酱中是否有酱汁,而不是意大利面酱中是否有酱汁,目前这就是它的作用,并且无论如何要实现这一目标吗?

由于

2 个答案:

答案 0 :(得分:1)

您可以使用isin方法:

{{1}}

答案 1 :(得分:0)

这就是你想要的吗?

s = pd.Series(["cat dog", "dog", "sheep"])

# check if 'cat' or 'dog' is present in the Series.
s.apply(lambda x: np.max(np.in1d(np.asarray(x.split()), ['cat', 'dog'])))
Out[785]: 
0     True
1     True
2    False
相关问题