Chris Dixon
Chris Dixon

Reputation: 1046

How to add a column to dataframe checking if a string is contained in some or any columns of that row?

df looks like this:

       food  triggered  numbers         snacks
0  sandwich      False        5           tofu
1     chips      False        2        seaweed
2       egg       True        7  yeah_whatever
3  whatever      False        1      bird food
4      milk       True       10     chia seeds

df = pd.DataFrame({'food': ['sandwich', 'chips', 'egg', 'whatever', 'milk'], 'triggered': [False, False, True, False, True], 'numbers' : [5, 2, 7, 1, 10], 'snacks': ['tofu', 'seaweed', 'yeah_whatever', 'bird food', 'chia seeds']})

and I want to add columns has_food_whatevers and has_any_whatevers checking if the string 'whatever' is contained (eg. 'yeah_whatever') in a range of columns or any columns of that row, to get:

       food  triggered  numbers         snacks  has_food_whatevers  has_any_whatevers
0  sandwich      False        5           tofu               False              False
1     chips      False        2        seaweed               False              False
2       egg       True        7  yeah_whatever               False               True
3  whatever      False        1      bird food                True               True
4      milk       True       10     chia seeds               False              False
    
desired_df = pd.DataFrame({'food': ['sandwich', 'chips', 'egg', 'whatever', 'milk'], 'triggered': [False, False, True, False, True], 'numbers' : [5, 2, 7, 1, 10], 'snacks': ['tofu', 'seaweed', 'yeah_whatever', 'bird food', 'chia seeds'], 'has_food_whatevers': [False, False, False, True, False], 'has_any_whatevers': [False, False, True, True, False]})

This SO thread shows how to select and drop relevant rows but I don't know how to add this as a column. Can I do this with .transform somehow?

Upvotes: 0

Views: 51

Answers (1)

BENY
BENY

Reputation: 323316

Let us try str.contains

df['has_food_whatevers'] = df['food'].str.contains('whatever')
df['has_any_whatevers'] = df[['food','snacks']].apply(lambda x : x.str.contains('whatever')).any(1)

Upvotes: 1

Related Questions