首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Pandas系列中过滤特定的单词(有变化)

在Pandas系列中过滤特定的单词(有变化)
EN

Stack Overflow用户
提问于 2019-06-05 07:27:04
回答 1查看 54关注 0票数 1

我有一个大型的dataframe,它的一个列中有一个单词的几个变体。我想根据我要找的特定单词过滤行。样本数据如下所示。在这里,我想过滤那些在“rows”列中有"create“这个词的行,而不是其中的一个子字符串,比如"recreate”或“recreate”。

注意事项:我只是在寻找一个在str.contains中应用的Regex解决方案

代码语言:javascript
运行
复制
In [4]: df = pd.DataFrame({"Resolution":["create profile", "recreate profile", "re-create profile", "created profile",
   ...: "re-created profile", "closed outlook and recreated profile", "purged outlook processes and created new profile
   ...: "], "Product":["Outlook", "Outlook", "Outlook", "Outlook", "Outlook", "Outlook", "Outlook"]})

In [5]: df
Out[5]:
                                         Resolution  Product
0                                    create profile  Outlook
1                                  recreate profile  Outlook
2                                 re-create profile  Outlook
3                                   created profile  Outlook
4                                re-created profile  Outlook
5              closed outlook and recreated profile  Outlook
6  purged outlook processes and created new profile  Outlook

我的尝试:

我已经能够过滤“重新创造”和“重新创造”(过去式不重要):

代码语言:javascript
运行
复制
In [13]: df[df.Resolution.str.contains("(?=.*recreate|re-create)(?=.*profile)")]
Out[13]:
                             Resolution  Product
1                      recreate profile  Outlook
2                     re-create profile  Outlook
4                    re-created profile  Outlook
5  closed outlook and recreated profile  Outlook

问:如何修改regex,使其只包含"create“而不是子字符串的行?就像这样:

代码语言:javascript
运行
复制
                                      Resolution  Product
0                                    create profile  Outlook
3                                   created profile  Outlook
6  purged outlook processes and created new profile  Outlook
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-05 07:28:24

为反转条件添加~

代码语言:javascript
运行
复制
df = df[~df.Resolution.str.contains("(?=.*recreate|re-create)(?=.*profile)")]
print (df)
                                          Resolution  Product
0                                     create profile  Outlook
3                                    created profile  Outlook
6  purged outlook processes and created new profile   Outlook
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56455922

复制
相关文章

相似问题

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