0

I have two dataframes I want to check if a column from first dataframe contains values that are in the column of second dataframe, and if it does, create a column and add 1 to the row where it contains a value from first column first df:

A header Another header
First apple
Second orange
third banana
fourth tea

desired output in second df second df:

A header Another header match
First sheet 0
Second chair 0
third apple 1
fourth orange 1
0

1 Answer 1

1

Use Series.isin and convert boolean to 1/0 values by casting:

df2['match'] = df2['Another header'].isin(df1['Another header']).astype(int)

Or by numpy.where:

df2['match'] = np.where(df2['Another header'].isin(df1['Another header']), 1, 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.