Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,句子正好包含这个词_Python_Pandas_Word - Fatal编程技术网

在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