python - 在 Python 中提取 Pandas 数据框的行

标签 python pandas dataframe

我已经将一个简单的查询日志导入到 Python 中的 pandas 数据框中(见图),并且想知道最有效的方法是提取包含“关键字”中包含的任何给定关键字的所有行' 列。

我可以遍历数据框 - 但我觉得使用数组/掩码可能是一种更快的方法。

非常感谢任何帮助。

enter image description here

最佳答案

您可以使用 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

关于python - 在 Python 中提取 Pandas 数据框的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16710999/

相关文章:

Python编码列表 'list'对象没有属性 'encode'

Python:如何将数字映射到列中的唯一项目(枚举唯一对象)?

python - 从 pandas 数据框中提取列,无需硬编码

python - 在自定义分隔符上拆分字符串列替换相邻的 NaN

在 dplyr 中重新编码给出错误 : Argument 2 must be named, 未命名

python - python 中的类方法 : default kwargs from attributes of self

python - 是否可以在没有 "any"框架的情况下使用 python 编写 Web 应用程序?

python - 调用另一个函数中定义的函数

python - pandas 类型转换的 h2o 框架

python - Pandas 数据框中的颜色单元格