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

Time:09-06

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?

CodePudding user response:

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

CodePudding user response:

Try:

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