首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >熊猫:选择与字符串匹配的行,并使用该单词创建一个新列。

熊猫:选择与字符串匹配的行,并使用该单词创建一个新列。
EN

Stack Overflow用户
提问于 2019-03-09 09:41:36
回答 2查看 195关注 0票数 0

熊猫:选择与字符串匹配的行,用该词创建新列,选择与字符串匹配的行,用该单词创建新列(找到)

代码语言:javascript
复制
list_provided=["mul","the","have", "then"]

我的数据看起来如何

代码语言:javascript
复制
id  text
a    simultaneous there the
b    simultaneous there
c    mul why

预期输出

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-09 09:43:57

我认为像这样的事情应该有效:

代码语言:javascript
复制
df['text'].apply(lambda x: [i for i in x.split() if i in list_provided])
票数 2
EN

Stack Overflow用户

发布于 2019-03-09 10:04:24

另一种使用regex模式的方法:

代码语言:javascript
复制
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]
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55075968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档