df( Pandas 数据框)具有三行。

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".

如何在str.contains方法上应用AND运算符,以便它仅捕获同时包含苹果和香蕉的字符串?
"apple and banana both are delicious"

我想抓取包含10-20个不同单词的字符串(葡萄,西瓜,浆果,橘子等)。

最佳答案

您可以按照以下步骤进行操作:

df[(df['col_name'].str.contains('apple')) & (df['col_name'].str.contains('banana'))]

关于python - Pandas 数据框str.contains()和运算,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37011734/

10-12 18:26