gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:42:32 26 4
gpt4 key购买 nike

我已经将一个简单的查询日志导入到 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com