python - 熊猫:选择与字符串匹配的行并使用该单词创建一个新列
问题描述
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
解决方案
使用正则表达式模式的另一种方法:
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]
推荐阅读
- reactjs - 我需要暂停我的自定义计时器并使用 rxjs 运算符从最后一个值开始
- javascript - 如何从另一个组件设置状态
- python - SpaCy 3:如何获取用于训练 en_core_web_sm 的原始数据?
- html - React Native - 输入顶部的标签
- python - 更新数据框中的多个单元格值
- angular - 从 SelectionModel 中恢复选择的 mat-tree
- database - 如何在集合 Firestore Flutter 中获得最低/最高价格
- javascript - 将 HTML 值传递给由多个按钮调用的 JavaScript 函数
- c++ - cmake、ccmake、make 和 make install 有什么区别?
- c++ - 具有软浮点的 MIPS 交叉编译器