Mufeez
Mufeez

Reputation: 55

How do I check if a value in column is present in other column in Python?

Let's take a data frame

Col1 Col2
First st
Second -th
Third rd
Fourth six
Fifth fif

Now I want to search if values in col2 are present in col1. Like it should be TRUE for the First, Third and Fifth rows but should be FALSE for the second and fourth rows.

Can you please help me with it?

Upvotes: 0

Views: 49

Answers (2)

ozacha
ozacha

Reputation: 1352

Try:

df.apply(lambda row: row["Col2"] in row["Col1"], axis='columns')

Upvotes: 1

jezrael
jezrael

Reputation: 862406

Use list comprehension with convert values to lower case:

df['new'] = [b.lower() in a.lower() for a, b in zip(df.Col1, df.Col2)]
print (df)
     Col1 Col2    new
0   First   st   True
1  Second  -th  False
2   Third   rd   True
3  Fourth  six  False
4   Fifth  fif   True

Upvotes: 2

Related Questions