I have the following data:
| country | code | continent | plants | invertebrates | vertebrates | total |
|---|---|---|---|---|---|---|
| Afghanistan | AFG | Asia | 5 | 2 | 33 | 40 |
| Albania | ALB | Europe | 5 | 71 | 61 | 137 |
| Algeria | DZA | Africa | 24 | 40 | 81 | 145 |
I want to add a hemisphere column that is determined by the continent that references a list. I want to do it using a custom function (and not using lambda).
I attempted the following:
northern = ['North America', 'Asia', 'Europe']
southern = ['Africa','South America', 'Oceania']
def hem(x,y):
if y in northern:
x = 'northern'
return x
elif y in southern:
x = 'southern'
return x
else:
x = 'Not Found'
return x
species_custom['hemisphere'] = species_custom.apply(hem, args=(species_custom['continent'],), axis=1)
I receive the following error:
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 0')
Any help is greatly appreciated.
hemis defined as taking two arguments but in theapplyyou only pass one. And when you do you are passing the fullcontinentcolumn to it. Probably not what you want.You could simplify by using nested
numpywhere.Result