'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?
Solution 1:[1]
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 )
Solution 2:[2]
You can also use ( for Pandas >= 0.13 ) :
filtered_df = df.query( '"Country Name" == ["brazil","poland","russia","countrydummy","usa"]' )
print(filtered_df )
Solution 3:[3]
I think that df.query is the best way for this kind of things
df = df.query(["Col2 == ['High','Medium']")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Roni Antonio |
| Solution 2 | Soumya Boral |
| Solution 3 | Pablo Santurio Alonso |
