Created
December 6, 2016 20:55
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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