I'm trying to create a script which removes all rows which have a matching negative and positive unique ID totaling to nil.
Is there are function/ module that could easily and efficiently achieve this goal without having to go about the df.iterrows route?
Original Dataset extract (full dataset has lots of different references and amounts)
| reference | amount |
|---|---|
| 5231 | 505 |
| 5231 | -505 |
| 5231 | 505 |
| 5231 | -505 |
| 5231 | -505 |
| 5231 | 505 |
| 5231 | 505 |
| 5231 | 505 |
I have created the ID column, however need to create a "Unique_ID_Count" column to identify the number of duplicates.
| reference | amount | ID | Unique_ID_Count |
|---|---|---|---|
| 5231 | 505 | 5231_505 | 5231_505_0 |
| 5231 | -505 | 5231_-505 | 5231_-505_0 |
| 5231 | 505 | 5231_505 | 5231_505_1 |
| 5231 | -505 | 5231_-505 | 5231_-505_1 |
| 5231 | -505 | 5231_-505 | 5231_-505_2 |
| 5231 | 505 | 5231_505 | 5231_505_2 |
| 5231 | 505 | 5231_505 | 5231_505_3 |
| 5231 | 505 | 5231_505 | 5231_505_4 |
Once I've identified my duplicates I need to remove all instances where there is a corresponding positive and negative duplicate with the same count. This removes all rows that net off to nil.
| reference | amount | ID | Unique_ID_Count |
|---|---|---|---|
| 5231 | 505 | 5231_505 | 5231_505_3 |
| 5231 | 505 | 5231_505 | 5231_505_4 |
Any help would be much appreciated as I feel like I'm going in circles thinking about how to achieve this.
Data:
{'reference': [5231, 5231, 5231, 5231, 5231, 5231, 5231, 5231],
'amount': [505, -505, 505, -505, -505, 505, 505, 505]}