【发布时间】:2016-08-28 22:44:11
【问题描述】:
我有一个三行的 df (Pandas Dataframe):
some_col_name
"apple is delicious"
"banana is delicious"
"apple and banana both are delicious"
函数df.col_name.str.contains("apple|banana") 将捕获所有行:
"apple is delicious",
"banana is delicious",
"apple and banana both are delicious".
如何将 AND 运算符应用于 str.contains() 方法,以便它只抓取包含“apple”和“banana”的字符串?
"apple and banana both are delicious"
我想抓取包含 10-20 个不同单词(葡萄、西瓜、浆果、橙子、...等)的字符串
【问题讨论】:
-
这个例子是玩具,因为你只有 K=2 个子字符串并且它们按顺序出现:苹果、香蕉。但您确实需要一种以任意顺序匹配 K=10-20 个子字符串的方法。具有多个前瞻断言的正则表达式是可行的方法(@Anzel 的解决方案)。
标签: python string pandas dataframe