pandas 数据帧 str.contains() AND 操作 [英] pandas dataframe str.contains() AND operation

查看:20
本文介绍了 pandas 数据帧 str.contains() AND 操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三行的 df(Pandas 数据框):

I have a df (Pandas Dataframe) with three rows:

some_col_name
"apple is delicious"
"banana is delicious"
"apple and banana both are delicious"

函数 df.col_name.str.contains("apple|banana") 将捕获所有行:

The function df.col_name.str.contains("apple|banana") will catch all of the rows:

"apple is delicious",
"banana is delicious",
"apple and banana both are delicious".

如何将 AND 运算符应用于 str.contains() 方法,以便它只抓取包含apple"和apple"的字符串.&香蕉"?

How do I apply AND operator to the str.contains() method, so that it only grabs strings that contain BOTH "apple" & "banana"?

"apple and banana both are delicious"

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

I'd like to grab strings that contains 10-20 different words (grape, watermelon, berry, orange, ..., etc.)

推荐答案

你可以这样做:

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

这篇关于 pandas 数据帧 str.contains() AND 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆