SpiralDev
SpiralDev

Reputation: 7321

Remove rows if it exists in the previous group

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

Upvotes: 1

Views: 58

Answers (1)

BENY
BENY

Reputation: 323226

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

Upvotes: 4

Related Questions