Python Pandas:多列的布尔索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17216153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Pandas: Boolean indexing on multiple columns
提问by LondonRob
despite there being at least twogoodtutorials on how to index a DataFrame in Python's pandaslibrary, I still can't work out an elegant way of SELECTing on more than one column.
尽管至少有两个关于如何在 Python库中索引 DataFrame 的好教程pandas,但我仍然无法找到一种优雅的方法来SELECT处理多个列。
>>> d = pd.DataFrame({'x':[1, 2, 3, 4, 5], 'y':[4, 5, 6, 7, 8]})
>>> d
x y
0 1 4
1 2 5
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2] # This works fine
x y
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2 & d['y']>7] # I had expected this to work, but it doesn't
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I have found (what I think is) a rather inelegant way of doing it, like this
我发现(我认为是)一种相当不优雅的方式,就像这样
>>> d[d['x']>2][d['y']>7]
But it's not pretty, and it scores fairly low for readability (I think).
但它并不漂亮,而且它的可读性得分相当低(我认为)。
Is there a better, more Python-tastic way?
有没有更好、更符合 Python 风格的方式?
采纳答案by Boud
It is a precedence operator issue.
这是一个优先运算符问题。
You should add extra parenthesis to make your multi condition test working:
您应该添加额外的括号以使您的多条件测试工作:
d[(d['x']>2) & (d['y']>7)]
This sectionof the tutorial you mentioned shows an example with several boolean conditions and the parenthesis are used.
您提到的教程的这一部分显示了一个带有几个布尔条件的示例,并使用了括号。
回答by TomAugspurger
There may still be a better way, but
可能还有更好的方法,但是
In [56]: d[d['x'] > 2] and d[d['y'] > 7]
Out[56]:
x y
4 5 8
works.
作品。

