Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Check whether a pandas dataframe contains rows with a value that exists in another dataframe.
# We have dataframe A with column name
# We have dataframe B with column name
# I want to see rows in A with name Y such that there exists rows in B with name Y.
# It's like set intersection.
intersected = reduce(lambda x, y: x | (A['name'] == y), [False] + list(B['name']))
intersection = A[intersected]
# other alternatives
intersection = pd.merge(A, B, how='inner', on=['name'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment