I have a customer table. I am trying to filter each ParentCustomerID based on multiple points they have and select a row based on the below conditions:
IF 0 points & negative points, select the row with the highest negative point (i.e. -30 > -20)
IF 0 points & positive points, select the row with the highest positive point
IF Positive & Negative Points, select the row with the highest positive point
IF Positive, 0 points, and Negative points, select the row with the highest positive point
IF 0 Points mark, select any row with 0 points
IF All Negative, select the row with the highest negative point (i.e. -30 > -20)
1:M relationship between ParentCustomerID and ChildCustomerID
| ParentCustomerID | ChildCustomerID | Points |
|---|---|---|
| 101 | 1 | 0.0 |
| 101 | 2 | -20.0 |
| 101 | 3 | -30.50 |
| 102 | 4 | 20.86 |
| 102 | 5 | 0.0 |
| 102 | 6 | 50.0 |
| 103 | 7 | 10.0 |
| 103 | 8 | 50.0 |
| 103 | 9 | -30.0 |
| 104 | 10 | -30.0 |
| 104 | 11 | 0.0 |
| 104 | 12 | 60.80 |
| 104 | 13 | 40.0 |
| 105 | 14 | 0.0 |
| 105 | 15 | 0.0 |
| 105 | 16 | 0.0 |
| 106 | 17 | -20.0 |
| 106 | 18 | -30.80 |
| 106 | 19 | -40.20 |
Output should be:
| ParentCustomerID | ChildCustomerID | Points |
|---|---|---|
| 101 | 3 | -30.50 |
| 102 | 6 | 50.0 |
| 103 | 8 | 50.0 |
| 104 | 12 | 60.80 |
| 105 | 16 | 0.0 |
| 106 | 19 | -40.20 |
Note: for the rows customer 105, any row can be chosen because they all have 0 points.
Note2: Points can be float and ChildCustomerID can be missing (np.nan)
I do not know how to group each ParentCustomerID, check the above conditions, and select a specific row for each ParentCustomerID.
Thank you in advance!
Code
How this works
abswith the absolute values ofPointspri(priority) corresponding to arithmetic signs(i.e, -1, 0, 1) of values inPoints, Important hack: replace0with-2so that zero always has least priority.ParentCustomerIDResult