Reputation: 13
I would like filter two or more countries in this dataframe, but I don't know how.
covid.filter(items = [ 'Country', 'Confirmed', 'Deaths'] ).where(covid.Country == 'Canada').groupby('Country').max()
Upvotes: 1
Views: 160
Reputation: 896
Assuming covid
is a Pandas dataframe. You could
covid.filter(items = [ 'Country', 'Confirmed', 'Deaths'] ).where(covid.Country.isin(['Canada', 'Brazil', 'US'])).groupby('Country').max()
Note condition covid.Country.isin(['Canada', 'Brazil', 'US'])
in where()
. It will match if covid.Country
is one of the listed.
EDIT: Fixed code because x in collection
wont work cause in
will use standard python truth-values. For pandas
these are considered ambiguous so you should use "bitwise" operations.
Upvotes: 1
Reputation: 71689
You can use Series.isin(values)
function of pandas series to check whether values are contained in Series.
Try this:
countries = ["US", "Canada"] #--> list of countries to be included
result = (covid.filter(items=['Country', 'Confirmed', 'Deaths'])
.where(covid["Country"].isin(countries))
.groupby('Country')
.max())
Upvotes: 2