我有一个要搜索的字符串列表。你知道吗
strings = ['Tea','Baseball','Onus']
我的数据帧是
itemid desc
0 101 tea leaves
1 201 baseball gloves
3 221 teas leaves from Onus Green Tea Co.
我想得到这样的东西,不考虑部分匹配
itemid desc matches
0 101 tea leaves [Tea]
1 201 baseball gloves [Baseball]
2 221 teas leaves from Onus Green Tea Co. [Tea, Onus]
我正在这么做
import re
df['desc'] = df.desc.str.split(' ')
df['desc'].str.findall('|'.join(strings),flags=re.IGNORECASE)
但它给了我一系列空逗号
0 [(, , , , , ), (, , , , , ), (, , , , , )]
1 [(, , , , , ), (, , , , , ), (, , , , , )]
2 [(, , , , , ), (, , , , , ), (, , , , , )]
请帮我解决这个问题。你知道吗
编辑:我不想要部分匹配。更新的例子反映了这一点。你知道吗
尝试将
contains
与regex交替使用:我们可以将
Series.str.findall
与regex ignore case标志(?i
)一起使用,这样就不必使用import re
要删除重复项,我们将字符串转换为大写,并生成
set
:编辑部分匹配
我们可以使用单词边界
\b
:您不需要吐出
desc
列。你知道吗输出:
相关问题 更多 >
编程相关推荐