Reputation: 6431
In pandas dataframe, how to fill the value of a column conditionally to values in an other column being part of a list ?
This is very similar to this SO question, but when I apply:
df['type'] = np.where(df['food'] in ['apple', 'banana', 'kiwi'], 'fruit', 'oth. food')
I got an error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I suppose the in
operator has been not overridden to work with vectors..
Upvotes: 6
Views: 1823
Reputation: 14689
this should work
df['type'] = np.where(df['food'].isin(['apple', 'banana', 'kiwi']), 'fruit', 'oth. food')
Upvotes: 7