Reputation: 693
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?
Upvotes: 0
Views: 2174
Reputation: 139342
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
Upvotes: 2