在python中,句子正好包含这个词
我想返回包含搜索列表中确切单词的句子在python中,句子正好包含这个词,python,pandas,word,Python,Pandas,Word,我想返回包含搜索列表中确切单词的句子 df = pd.read_excel('C:/Test 1012/UOI.xlsx') a = df['Content'] searchfor =['hot' ,'yes' and 200 more words in it] b = a[a.str.contains('|'.join(searchfor))] print(b) 例如: Content = ['the photo is good','nice picture'...] 结果不应该打印任何
df = pd.read_excel('C:/Test 1012/UOI.xlsx')
a = df['Content']
searchfor =['hot' ,'yes' and 200 more words in it]
b = a[a.str.contains('|'.join(searchfor))]
print(b)
例如:
Content = ['the photo is good','nice picture'...]
结果不应该打印任何句子,但是,“照片”包含单词“热”,结果给我“照片很好”。有人知道如何解决这个问题吗?我只希望得到的结果准确地包含searchfor列表中的单词。使用为searchfor的每个值添加的单词边界:
df = pd.DataFrame({'Content':['the photo is good','nice picture']})
print (df)
Content
0 the photo is good
1 nice picture
searchfor =['hot','yes','nice']
pat = '|'.join(r"\b{}\b".format(x) for x in searchfor)
b = df.loc[df['Content'].str.contains(pat), 'Content']
#your solution
#b = a[a.str.contains(pat)]
print (b)
1 nice picture
Name: Content, dtype: object