Skip to content

Instantly share code, notes, and snippets.

@sainathadapa
Created May 9, 2018 10:00
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save sainathadapa/eb3303975196d15c73bac5b92d8a210f to your computer and use it in GitHub Desktop.
Save sainathadapa/eb3303975196d15c73bac5b92d8a210f to your computer and use it in GitHub Desktop.
anti-join-pandas
import pandas as pd
def anti_join(x, y, on):
"""Return rows in x which are not present in y"""
ans = pd.merge(left=x, right=y, how='left', indicator=True, on=on)
ans = ans.loc[ans._merge == 'left_only', :].drop(columns='_merge')
return ans
def anti_join_all_cols(x, y):
"""Return rows in x which are not present in y"""
assert set(x.columns.values) == set(y.columns.values)
return anti_join(x, y, x.columns.tolist())
@echennh-zz
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment