1

I have a data set which looks something like this.

**name**       **url**               **title**
Microsoft  asdfgaethgaetgh   Microsoft Is a big company
Apple      aeiurghp          iphone is a Apple product
Google     iailsu            Yahoo, Bing Profit Rises

I would like to use a flag where 'True' would be if the cell value of 'name' is present in the cell value of 'title'. Otherwise, 'False'

I am using something like this

df['flag'] = (df.name).isin(df.title)

But this gives all the flags as 'False', whereaas the first two flags should be 'True'

How can i take care of this?

1
  • 3
    What is isin? Just use the in operator: 'Microsoft' in 'Microsoft is a big company' Commented Mar 22, 2016 at 7:27

5 Answers 5

3

This can also be used :

criteria = lambda row : row['name'] in row['title']
df['flag'] = df.apply(criteria, axis =1)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use df['flag'] = True if df.name in df.title else False The code uses single line if else condition

3 Comments

As denis's answer shows, there's no need to specify values (True and False in this case) for something that already resolves to those values.
I thought this was more representative :)
It's as representative as print('alice' if usr_name=='alice' else 'bob' if usr_name=='bob' else 'charlie' if usr_name=='charlie' else...). In other words, no.
0

isin will work only if you want to search name within list of names. To search for substring use str.contains instead

df.title.str.contains(df.name)

Comments

0

Sample like this;

if df[name] in df[title]:
                        return true

Comments

0

Your code should be like this

df['flag'] = (df.name in df.title)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.