gpt4 book ai didi

python - 如何在 pandas query() 中使用 str 方法

转载 作者:行者123 更新时间:2023-11-28 18:22:21 25 4
gpt4 key购买 nike

在 pandas 查询中使用 str 方法似乎有正确和错误的方法。为什么第一个查询按预期工作但第二个查询失败:

>>> import pandas
>>> data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
... 'year': [2012, 2012, 2013, 2014, 2014],
... 'coverage': [25, 94, 57, 62, 70]}
>>> df = pandas.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
>>> print(df.query('name.str.slice(0,1)=="J"'))
coverage name year
Cochice 25 Jason 2012
Maricopa 62 Jake 2014
>>>
>>> print(df.query('name.str.startswith("J")'))
<lines omitted>
TypeError: 'Series' objects are mutable, thus they cannot be hashed

最佳答案

试试这个技巧:

In [62]: df.query("name.str.startswith('J').values")
Out[62]:
coverage name year
Cochice 25 Jason 2012
Maricopa 62 Jake 2014

或者,您可以指定 engine='python':

In [63]: df.query("name.str.startswith('J')", engine='python')
Out[63]:
coverage name year
Cochice 25 Jason 2012
Maricopa 62 Jake 2014

时间:对于 50 万行 DF:

In [68]: df = pd.concat([df] * 10**5, ignore_index=True)

In [69]: df.shape
Out[69]: (500000, 3)

In [70]: %timeit df.query("name.str.startswith('J')", engine='python')
1 loop, best of 3: 583 ms per loop

In [71]: %timeit df.query("name.str.startswith('J').values")
1 loop, best of 3: 587 ms per loop

In [72]: %timeit df[df.name.str.startswith('J')]
1 loop, best of 3: 571 ms per loop

In [74]: %timeit df.query('name.str.slice(0,1)=="J"')
1 loop, best of 3: 482 ms per loop

关于python - 如何在 pandas query() 中使用 str 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44230272/

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