如何从Pandas数据框中过滤包含字符串模式的行 [英] How to filter rows containing a string pattern from a Pandas dataframe

查看:65
本文介绍了如何从Pandas数据框中过滤包含字符串模式的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们在Python Pandas中有一个数据框,如下所示:

Assume we have a data frame in Python Pandas that looks like this:

df = pd.DataFrame({'vals': [1, 2, 3, 4], 'ids': [u'aball', u'bball', u'cnut', u'fball']})

或者,以表格形式:

ids    vals
aball   1
bball   2
cnut    3
fball   4

如何过滤包含关键字"ball"的行?例如,输出应为:

How do I filter rows which contain the key word "ball?" For example, the output should be:

ids    vals
aball   1
bball   2
fball   4

推荐答案

In [3]: df[df['ids'].str.contains("ball")]
Out[3]:
     ids  vals
0  aball     1
1  bball     2
3  fball     4

这篇关于如何从Pandas数据框中过滤包含字符串模式的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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