如何从 Pandas 数据框中过滤包含字符串模式的行 [英] How to filter rows containing a string pattern from a Pandas dataframe
本文介绍了如何从 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屋!
查看全文