简体   繁体   English

在Python中提取Pandas数据框的行

[英]Extracting rows for a Pandas dataframe in Python

I have imported a simple query log into a pandas dataframe in Python (see image), and would like to know what the most efficient way is to extract all of the rows that contain any given keyword that is contained in the 'Keyword' column. 我已经将一个简单的查询日志导入到Python中的pandas数据框中(请参见图片),并且想知道最有效的方法是提取所有包含“关键字”列中包含任何给定关键字的行。

I could iterate over the dataframe - but have a feeling there might be a quicker way using arrays/masks. 我可以遍历数据框-但是感觉使用数组/掩码可能会有更快的方法。

Any help greatly appreciated. 任何帮助,不胜感激。

在此处输入图片说明

You can use str.contains , for example: 您可以使用str.contains ,例如:

In [1]: df = pd.DataFrame([['abc', 1], ['cde', 2], ['efg', 3]])

In [2]: df
Out[2]:
     0  1
0  abc  1
1  cde  2
2  efg  3

In [3]: df[0].str.contains('c')
Out[3]:
0     True
1     True
2    False
Name: 0, dtype: bool

In [4]: df[df[0].str.contains('c')]
Out[4]:
     0  1
0  abc  1
1  cde  2
[3]: df = DataFrame(dict(A = ['foo','bar','bah','bad','bar'],B = range(5)))

In [4]: df
Out[4]: 
     A  B
0  foo  0
1  bar  1
2  bah  2
3  bad  3
4  bar  4

In [5]: select = Series(['bar','bah'])

In [6]: df[df.A.isin(select)]
Out[6]: 
     A  B
1  bar  1
2  bah  2
4  bar  4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM