1

I have a GroupBy object. I want to to remove rows from current group if the same row exists in the previous group. Let's say this is (n-1)th group:

    A   B
0   foo 0
1   baz 1
2   foo 1
3   bar 1

And this n-th group

    A   B
0   foo 2
1   foo 1
2   baz 1
3   baz 3

After dropping all duplicates. Result of n-th group:

    A   B
0   foo 2
3   baz 3

EDIT:

I would like to achieve it without loop if possible

SpiralDev
  • 7,011
  • 5
  • 28
  • 42

1 Answers1

4

I am using merge with indicator here

yourdf=dfn.merge(df1,indicator=True,how='left').loc[lambda x : x['_merge']!='both']
yourdf
     A  B     _merge
0  foo  2  left_only
3  baz  3  left_only
#yourdf.drop('_merge',1,inplace=True)

Since it is GrouBy Object so you can do with for loop here , using above code for n times

BENY
  • 317,841
  • 20
  • 164
  • 234
  • Any way to achieve it without loop? – SpiralDev Mar 28 '19 at 01:18
  • 1
    @DeveScie yes, it will need for loop , but for loop not always bad practice :-) https://stackoverflow.com/questions/54028199/for-loops-with-pandas-when-should-i-care – BENY Mar 28 '19 at 01:18