Check if the values of a certain Dataframe column exist in a certain list using np.Where

Asked

Viewed 658 times

0

Hello! I’ve been trying to check if a certain value present in a certain dataframe column is present in a list, using np.Where.

In other words, it would look like this:

 df = {
    'Pais': ['Brazil', 'Colombia', 'Argentina', 'EUA'],
    'PIB' : [1000, 1056, 1070, 410]
}
df = pd.DataFrame(df)

america_do_sul = ['Brazil', 'Colombia', 'Argentina', 'Peru', 'Venezuela', 'Ecuador', 'Bolivia', 'Paraguay', 'Uruguay']
df['Sulamericano'] = np.where(df['Pais'] in america_do_sul, 1, 0)

The idea of code is to create a column in my dataframe called 'South American' where it has the value '1' for yes and '0' for not.

However, when this code is executed it generates the following error:

Valueerror: The Truth value of a Series is ambiguous. Use a.Empty, a. bool(), a.item(), a.any() or a.all().

  • Note: The figures in the GDP column are fictitious!

2 answers

2

I know you asked to use np.where, but here’s a solution that uses a slightly more readable syntax, in my opinion:

df['Sulamericano'] = df['Pais'].isin(america_do_sul).astype(int)

1


Just change the way you compare to the list.

Put it like this:

df['Sulamericano'] = np.where(np.isin(df['Pais'], america_do_sul), 1, 0)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.