Chris90
Chris90

Reputation: 1998

Create a new Boolean True or False column if a column has a certain string or value?

I have a df column below as:

Comments
Yes - Verizon
Verizon

Does not contain 
T-Mobile
Standby, call for more
Verizon Software

How can I create new column that says True or False if the Comments column contains the word Verizon anywhere in that row?

Expected Output:

Comments         |  Has Verizon?
Yes - Verizon          True
Verizon                True
                       False
Does not contain       False
T-Mobile               False
Standby, call for more False
Verizon Software       True

Upvotes: 0

Views: 73

Answers (2)

Red
Red

Reputation: 27567

If you are familiar with list comprehensions, then you can do the same to your datafram columns:

df['Comments'] = [True if 'Verizon' in x else False for x in df['Comments']]

Though this is not very efficient, @Ami Tavory's answer is spot on.

Upvotes: 1

Ami Tavory
Ami Tavory

Reputation: 76297

df.Comments.str.contains('Verizon')

should do it. This causes to apply the string-column method contains.

Upvotes: 3

Related Questions