pandas 使用熊猫过滤多个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35164019/
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
Filter Multiple Values using pandas
提问by DataNoob
I am using Python and Pandas. I have a df
that works similar to this:
我正在使用 Python 和 Pandas。我有一个df
与此类似的工作:
+--------+--------+-------+
| Col1 | Col2 | Col 3 |
+--------+--------+-------+
| Team 1 | High | Pizza |
| Team 1 | Medium | Sauce |
| Team 1 | Low | Crust |
+--------+--------+-------+
I would like to filter the df
so that I only see High or Medium from Col2
.
我想过滤df
以便我只能从Col2
.
This is what I have tried with no luck
这是我在没有运气的情况下尝试过的
df = df.loc[df['Col 2'] == 'High' | (df['Col2'] == 'Medium')]
This is the error I am getting
这是我得到的错误
cannot compare a dtyped [bool] array with a scalar of type [bool]
Any ideas how to make this work and what that error means?
任何想法如何使这项工作以及该错误意味着什么?
回答by Stefan
You are missing a pair of parentheses to get comparable items on both sides of the |
operator - which has higher precedence than ==
(see docs):
您缺少一对括号来获取|
运算符两侧的可比较项- 其优先级高于==
(参见文档):
df = df.loc[(df['Col 2'] == 'High') | (df['Col2'] == 'Medium')]