0

I would like to create a new column with a numerical value assigned based on whether pet1 contains the word 'cat' or the word 'dog'

    pet1           
0   dog             
1   dog;cat;rabbit  
2   cat;dog         
3   manbearpig      
4   hippo           

I would like the end result to be as follows:

    pet1            points
0   dog             5
1   dog;cat;rabbit  5
2   cat;dog         5
3   manbearpig      0
4   hippo           0 

How do I do this?

0

1 Answer 1

2

You can use the string method contains for this.
Starting from this dataframe:

In [96]: df
Out[96]:
             pet1
0             dog
1  dog;cat;rabbit
2         cat;dog
3      manbearpig
4           hippo

You can check if each element contains the substring 'dog':

In [97]: df['pet1'].str.contains('dog')
Out[97]:
0     True
1     True
2     True
3    False
4    False
Name: pet1, dtype: bool

and then multiply by 5 to end up with your desired result:

In [98]: df['pet1'].str.contains('dog') * 5
Out[98]:
0    5
1    5
2    5
3    0
4    0
Name: pet1, dtype: int32
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.