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

时间:2017-05-28 17:47:42

标签: python string pandas

在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

1 个答案:

答案 0 :(得分:1)

尝试这个技巧:

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

时间:表示500K行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