使用pandas过滤多个值

时间:2016-02-02 21:22:01

标签: python pandas filtering

我正在使用Python和Pandas。我的df与此类似:

 +--------+--------+-------+
 |  Col1  |  Col2  | Col 3 |
 +--------+--------+-------+
 | Team 1 | High   | Pizza |
 | Team 1 | Medium | Sauce |
 | Team 1 | Low    | Crust |
 +--------+--------+-------+

我想过滤df,以便我只看到Col2中的高或中。

这就是我没有运气的尝试

 df = df.loc[df['Col 2'] == 'High' | (df['Col2'] == 'Medium')]

这是我得到的错误

 cannot compare a dtyped [bool] array with a scalar of type [bool]

任何想法如何使这项工作和错误意味着什么?

3 个答案:

答案 0 :(得分:21)

这也很好,更pythonic

country_list = ['brazil','poland','russia','countrydummy','usa']

filtered_df = df[df['Country Name'].isin(country_list)]
print(filtered_df )

答案 1 :(得分:2)

您缺少一对括号,以便在|运算符的两边获得可比项目,其优先级高于== (see docs)

df = df.loc[(df['Col 2'] == 'High') | (df['Col2'] == 'Medium')]

答案 2 :(得分:0)

您也可以使用(对于 Pandas >= 0.13 ):

filtered_df = df.query( '"Country Name" == ["brazil","poland","russia","countrydummy","usa"]' )

print(filtered_df )