How do I assign values based on content of columns?

Question:

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?

Asked By: Eric

||

Answers:

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
Answered By: joris
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.