Skip to content

Instantly share code, notes, and snippets.

@ksaliya
Forked from sainathadapa/anti_join.py
Created January 17, 2021 18:14
Show Gist options
  • Save ksaliya/f569b3452fb3e5144c9d9770b9770870 to your computer and use it in GitHub Desktop.
Save ksaliya/f569b3452fb3e5144c9d9770b9770870 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())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment