Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Multiple Values using pandas

I am using Python and Pandas. I have a df that works similar to this:

 +--------+--------+-------+
 |  Col1  |  Col2  | Col3 |
 +--------+--------+-------+
 | 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.

This is what I have tried with no luck

 df = df.loc[df['Col2'] == '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?

like image 321
DataNoob Avatar asked Feb 02 '16 21:02

DataNoob


1 Answers

This works as well, more pythonic

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

filtered_df = df[df['Country Name'].isin(country_list)]
print(filtered_df )
like image 147
Roni Antonio Avatar answered Sep 19 '22 19:09

Roni Antonio