How to conditionally create Pandas column using isin?

1.3k views Asked by At

I have the following dataframe:

{'population': {0: '38,928,346', 1: '2,877,797', 2: '43,851,044', 3: '77,265', 4: '32,866,272', 5: '97,929', 6: '45,195,774', 7: '2,963,243', 8: '25,499,884', 9: '9,006,398', 10: '10,139,177', 11: '393,244', 12: '1,701,575', 13: '164,689,383', 14: '287,375', 15: '9,449,323', 16: '11,589,623'}, 'index': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10, 10: 11, 11: 12, 12: 13, 13: 14, 14: 15, 15: 16, 16: 17}, 'country': {0: 'Afghanistan', 1: 'Albania', 2: 'Algeria', 3: 'Andorra', 4: 'Angola', 5: 'Antigua and Barbuda', 6: 'Argentina', 7: 'Armenia', 8: 'Australia', 9: 'Austria', 10: 'Azerbaijan', 11: 'Bahamas', 12: 'Bahrain', 13: 'Bangladesh', 14: 'Barbados', 15: 'Belarus', 16: 'Belgium'}}

And I need to create a new column 'case statement' style:

  countryList=['Albania', 'Angola', 'Australia']

  df['country1'] = (df['country'] if [df['country'].isin(countryList)] else 'Other')

The new column should only list those three countries in countryList, or say 'Other'. But when I run the code above it just duplicates the original column. This is something I have a frequent need for in processing data, and whenever I search I am unable to find anything that doesn't involve a loop which I would like to avoid.

I'm hoping for a one-line, easy to understand and straight forward way that uses the ISIN function to essentially do what I would normally do in a sql case statement.

EDIT: The link that suggests this is a duplicate links to a page where isin is not used in a single answer. I specifically asked how to do this using isin on the original question and would have only accepted a different solution if using isin wasn't possible.

1

There are 1 answers

1
Quang Hoang On BEST ANSWER

Use where:

df['country1'] = df['country'].where(df['country'].isin(countryList), 'Other')

or np.where:

df['country1'] = np.where(df['country'].isin(countryList), df['country'], 'Other')

Output:

     population  index              country   country1
0    38,928,346      1          Afghanistan      Other
1     2,877,797      2              Albania    Albania
2    43,851,044      3              Algeria      Other
3        77,265      4              Andorra      Other
4    32,866,272      5               Angola     Angola
5        97,929      6  Antigua and Barbuda      Other
6    45,195,774      7            Argentina      Other
7     2,963,243      8              Armenia      Other
8    25,499,884      9            Australia  Australia
9     9,006,398     10              Austria      Other
10   10,139,177     11           Azerbaijan      Other
11      393,244     12              Bahamas      Other
12    1,701,575     13              Bahrain      Other
13  164,689,383     14           Bangladesh      Other
14      287,375     15             Barbados      Other
15    9,449,323     16              Belarus      Other
16   11,589,623     17              Belgium      Other