I have a pandas dataframe that looks as follows:
X Y
71455 [334.0, 319.0, 298.0, 323.0]
71455 [3.0, 8.0, 13.0, 10.0]
57674 [54.0, 114.0, 124.0, 103.0]
I want to perform an aggregate groupby
that adds the lists stored in the Y columns element-wise. Code I have tried:
df.groupby('X').agg({'Y' : sum})
The result is the following:
Y
X
71455 [334.0, 319.0, 298.0, 323.0, 75.0, 55.0, ...
So it has concatenated the lists and not sum them element-wise. The expected result however is:
X Y
71455 [337.0, 327.0, 311.0, 333.0]
57674 [54.0, 114.0, 124.0, 103.0]
I have tried different methods, but could not get this to work as expected.