Pedro Alves
Pedro Alves

Reputation: 1054

Python - Add OR Operator on DataFrame Apply function

I've this dataframe:

word, string1, string2
SQL, SQL is good, Programming
Java, Programming, Java is good
C#, Programming, Programming

I've a column that give a boolean if my column word values is present on my column string1:

data['res'] = data.apply(lambda x: x.word in x.string1
                               if (x.string1 == x.string1) and (x.word == x.word)
                               else False)

But what I want is to see if the column word value is present on column string1 OR in string2? Something like this:

data['res'] = data.apply(lambda x: x.word in x.string1
                               if (x.string1 == x.string1) and (x.word == x.word)
                               else (x.word in x.string2
                                    if (x.string2 == x.string2) and (x.word == x.word))axis=1)
                    else False)

What I want is:

word, string1, string2, res
SQL, SQL is good, Programming, True
Java, Programming, Java is good, True
C#, Programming, Programming, False

Is this possible to do?

Thanks!

Upvotes: 0

Views: 335

Answers (2)

jezrael
jezrael

Reputation: 862911

Simpliest is join both columns and add another filter:

data['res'] = data.apply(lambda x: x.word in x.string1 + x.string2
                               if (x.string1 == x.string1 ) and 
                                  (x.word == x.word) and 
                                  (x.string1 == x.string1)
                               else False, axis=1)

Upvotes: 1

anky
anky

Reputation: 75080

You need to check if the string in column 1 is present in any other columns, with any() over axis=1:

df.apply(lambda x:x.str.contains(x.word),axis=1).iloc[:,1:].any(axis=1)

0     True
1     True
2    False

Full code:

df=df.assign(res=df.apply(lambda x:x.str.contains(x.word),axis=1).iloc[:,1:].any(axis=1))

   word       string1        string2    res
0   SQL   SQL is good    Programming   True
1  Java   Programming   Java is good   True
2    C#   Programming    Programming  False

Upvotes: 1

Related Questions