I came up with a solution using groupby :
groupped = df.groupby(['A', 'B']).size().reset_index().rename(columns={0: 'count'}) uniques = groupped[groupped['count'] == 1] duplicates = df[~df.index.isin(uniques.index)]
Duplicates now have the correct result:
ABC 2 foo 1 B 3 bar 1 A
Also, my initial attempt at the question can be fixed by simply adding keep=False to the drop_duplicates method:
# Load Dataframe df = pd.DataFrame({"A":["foo", "foo", "foo", "bar"], "B":[0,1,1,1], "C":["A","A","B","A"]}) uniques = df[['A', 'B']].drop_duplicates(keep=False) duplicates = df[~df.index.isin(uniques.index)]
Please answer @jezrael, I think this is safer (?) Since I use pandas indices here.
source share