1

I'm working on a project where I'm would like to use 2 lambda functions to find a match in another column. I created a dummy df with the following code below:

df = pd.DataFrame(np.random.randint(0,10,size=(100, 4)), columns=list('ABCD'))

Now I would like to find column A matches in column B.

df['match'] = df.apply(lambda x: x['B'].find(x['A']), axis=1).ge(0)

Now I would like to add an extra check where I'm also checking if column C values appear in column D:

df['match'] = df.apply(lambda x: x['D'].find(x['C']), axis=1).ge(0)

I'm searching for a solution where I can combine these 2 lines of code that is a one-liner that could be combined with an '&' operator for example. I hope this helps.

0

1 Answer 1

1

You can use and operator instead.

df['match'] = df.apply(lambda x: (x['B'] == x['A']) and (x['D'] == x['C']), axis=1).ge(0)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.