Home > Enterprise >  I want to check a value in column A is also present in column B
I want to check a value in column A is also present in column B

Time:02-22

Example data set below,

df1= ['A','B','C','D']
df2 = ['B','D','A','C']

how do i check if any value in df[1] match any value in df[2]

CodePudding user response:

Try using isin:

df[1].isin(df[2]).any()

CodePudding user response:

Yes, you can use the in keyword in python

df1= ['A','B','C','D']
df2 = ['B','D','A','C']

print(df1[0] in df2) #prints True becuase df1[0] is in df2

CodePudding user response:

Using numpy

 np.any([True if ix1 == ix2 else False for ix1 in df[1].unique() for ix2 in df[2].unique()])

CodePudding user response:

intersection() might help, it will create a set containing all values ​​existing in both df1 and df2:

df1= ['A','B','C','D']
df2 = ['B','D','A','C']

output = set(df1).intersection(set(df2))
# output = {'D', 'A', 'B', 'C'}

CodePudding user response:

You can use merge:

print(df1.merge(df2, on='col1', how='inner'))
  • Related