Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pandas query()中使用str方法_Python_String_Pandas - Fatal编程技术网

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

Python 如何在pandas query()中使用str方法,python,string,pandas,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

在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
导入熊猫 >>>数据={'name':['Jason','Molly','Tina','Jake','Amy'], .“年份”:[2012、2012、2013、2014、2014], …“覆盖范围”:[25,94,57,62,70]} >>>数据帧(数据,索引=['Cochice','Pima','Santa Cruz','Maricopa','Yuma']) >>>打印(df.query('name.str.slice(0,1)=“J”)) 覆盖范围名称年份 2012年12月25日 马里科帕62杰克2014 >>> >>>打印(df.query('name.str.startswith(“J”))) TypeError:“Series”对象是可变的,因此无法对其进行散列 试试这个技巧:

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

奇怪。。我复制了你的代码,它工作得很好。你做了什么来操纵原始df吗?@AndrewL,你的熊猫版本是什么?我可以使用Pandas 0.19.2和0.20.1Interesting(同样是在mac上运行的0.19.2)重现这个错误(
df.query('name.str.startswith(“J”)))
)!这就解释了为什么它对你有用。如果未安装
numexpr
,则会安装
df.query(“…”,engine='python')
。默认引擎是
numexpr
,对于数值计算,它应该更快operations@MaxU迷人的和伟大的知道!谢谢你@MaxU。添加engine='python'就可以做到这一点。我想知道为什么str.startswith需要它,而str.slice不需要它。@CarlosE,我认为它与
numexpr
有关,后者是
DataFrame.query()的默认引擎