While iterating through the variableA column, I want to generate a new column that is the sum of values whenever a row in either variableA or variableB equals the current row values of variableA. Example data:
values variableA variableB
0 134 1 3
1 12 2 6
2 43 1 2
3 54 3 1
4 16 2 7
I can select the sum of values whenever variableA matches the current row of variableA using:
df.groupby('variableA')['values'].transform('sum')
but selecting the sum of values whenever variableB matches the current row of variableA eludes me. I tried .loc but it doesn't seem to play well with .groupby. The expected output would be as follows:
values variableA variableB result
0 134 1 3 231
1 12 2 6 71
2 43 1 2 231
3 54 3 1 188
4 16 2 7 71
Thanks!

