Skip to content

Instantly share code, notes, and snippets.

@aladagemre
Created December 6, 2016 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aladagemre/e8db9a23724adbedb5002c7a0ce703df to your computer and use it in GitHub Desktop.
Save aladagemre/e8db9a23724adbedb5002c7a0ce703df to your computer and use it in GitHub Desktop.
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'])
intersection.dropna(inplace=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment