I am trying to subtract df A from df B where the column mapping is based on a 3rd mapping data frame. In this example, B should be subtracted from x1 and A should be subtracted from x2.
This can be done with loops and some other dirty methods, but I was wondering if there is a more concise way to do that.
Dataframe a
| date | A | B |
|---|---|---|
| 12/31/2019 | 0.1 | 0.4 |
| 12/31/2020 | 0.3 | 0.6 |
Dataframe b
| date | x1 | x2 | x3 |
|---|---|---|---|
| 12/31/2019 | 1.0 | 0.8 | 1.0 |
| 12/31/2020 | 0.4 | 0.7 | 1.5 |
Dataframe c
| From | To |
|---|---|
| x1 | B |
| x2 | A |
| x3 | A |
Required result
| date | x1 | x2 | x3 |
|---|---|---|---|
| 12/31/2019 | 0.6 | 0.7 | 0.9 |
| 12/31/2020 | -0.2 | 0.4 | 1.2 |