I'd like to sum over all remaining entries in a 2-d phase space. For a case where I have a 2d multi-index and I want to get the cumsum over each v1 individually, doing the below will suffice:
df['output'] = df.loc[::-1].groupby(['v1'])['value'].cumsum()[::-1]
| v1 | v2 | value | output |
|---|---|---|---|
| 1 | 1 | 1 | 3 |
| 1 | 2 | 1. | 2. |
| 1 | 3 | 1 | 1. |
| 2 | 1 | 1. | 3. |
| 2 | 2 | 1. | 2. |
| 2 | 3. | 1. | 1. |
| 3 | 1 | 1. | 3. |
| 3 | 2 | 1. | 2. |
| 3 | 3 | 1. | 1. |
But I'd like to instead integrate over v1 and v2 simultaneously. e.g. if v1=2 and v2=3, I'd like the result (v1=2 & v2=3) + (v1=3 & v2&3). Or for the same table, the desired result:
| v1 | v2 | value | output |
|---|---|---|---|
| 1 | 1 | 1 | 9 |
| 1 | 2 | 1. | 6. |
| 1 | 3 | 1 | 3. |
| 2 | 1 | 1. | 6. |
| 2 | 2 | 1. | 4. |
| 2 | 3. | 1. | 2. |
| 3 | 1 | 1. | 3. |
| 3 | 2 | 1. | 2. |
| 3 | 3 | 1. | 1. |
Any ideas how to do this?