Reputation: 19
Here is a part of df:
NUMBER MONEY
12345 20
12345 -20
123456 10
678910 7.6
123457 3
678910 -7.6
I want to drop rows which have the same NUMBER but opposite money.
The ideal outcome would like below:
NUMBER MONEY
123456 10
123457 3
Upvotes: 0
Views: 62
Reputation: 5785
Try this,
>>> df[~df.assign(MONEY=df.MONEY.abs()).duplicated(keep=False)]
Output:
NUMBER MONEY
2 123456 10.0
4 123457 3.0
From comments(by @piRSquared):
By using assign, a new column was added to df
and the subsequent drop_duplicates
takes into account both NUMBER
and the absolute value of MONEY
df[~df.assign(MONEY=df.MONEY.abs()).duplicated(keep=False)]
Upvotes: 1
Reputation: 75080
If there are just 2 rows in a group, use:
df[df.groupby('NUMBER').MONEY.transform('sum').ne(0)]
NUMBER MONEY
2 123456 10.0
4 123457 3.0
Upvotes: 1