I have a pandas dataframe and i need to subtract rows if they have the same group.
Input Dataframe:
| A | B | Value |
|---|---|---|
| A1 | B1 | 10.0 |
| A1 | B1 | 5.0 |
| A1 | B2 | 5.0 |
| A2 | B1 | 3.0 |
| A2 | B1 | 5.0 |
| A2 | B2 | 1.0 |
Expected Dataframe:
| A | B | Value |
|---|---|---|
| A1 | B1 | 5.0 |
| A1 | B2 | 5.0 |
| A2 | B1 | -2.0 |
| A2 | B2 | 1.0 |
Logic: For example the first and second rows of the dataframe are in group A1 and B1 so the value must be 10.0 - 5.0 = 5.0. 4º and 5º rows have the same group as well so the value must be 3.0 - 5.0 = -2.0.
Only subtract rows if they have same A and B value.
Thank you!