Isaac Oliveira
Isaac Oliveira

Reputation: 13

Return list with several country names

I would like filter two or more countries in this dataframe, but I don't know how.

image

covid.filter(items = [ 'Country', 'Confirmed', 'Deaths'] ).where(covid.Country == 'Canada').groupby('Country').max()

Upvotes: 1

Views: 160

Answers (2)

DarK_FirefoX
DarK_FirefoX

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

Shubham Sharma
Shubham Sharma

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

Related Questions