首页 > 解决方案 > 熊猫:选择与字符串匹配的行并使用该单词创建一个新列

问题描述

Pandas:选择与字符串匹配的行并使用该单词创建一个新列我想创建一个新列选择与字符串匹配的行并使用该单词创建一个新列(找到)

list_provided=["mul","the","have", "then"]

我的数据框看起来如何

id  text
a    simultaneous there the
b    simultaneous there
c    mul why

预期产出

id  text                        found
1    simultaneous there the      the
2    simultaneous there         
3    mul why                     mul
4    have the                    have, the 
5    then the late               then,the

标签: pythonpandas

解决方案


使用正则表达式模式的另一种方法:

pat = r'\b' + r'\b|\b'.join(list_provided) + r'\b'

df['found'] = df.text.str.findall(pat)


  id                    text        found
0  a  simultaneous there the        [the]
1  b      simultaneous there           []
2  c                 mul why        [mul]
3  d                have the  [have, the]
4  e           then the late  [then, the]

推荐阅读