Lilly
Lilly

Reputation: 988

Check if one column value is in another column in pandas

I want to compare one column with another column in the same dataframe. Not just between adjacent value but iterating through every value in Col1 and see if it exists in Col2 values.

Col1        Col2    exists
cat         pig     true
a           cat     false
pig         b       true
mat         axe     false

Thanks.

Upvotes: 2

Views: 5866

Answers (1)

Pulkit Jha
Pulkit Jha

Reputation: 1729

Col1_value = set(df['Col1'].unique())
df['exists'] = df['Col2'].map(lambda x : True if x in Col1_value  else False) 

Upvotes: 2

Related Questions