Is there any way of making a sum on the columns after grouping in pandas data frame? For example I have the following data frame:
ID W_1 W_2 W_3
1 0.1 0.2 0.3
1 0.2 0.4 0.5
2 0.3 0.3 0.2
2 0.1 0.3 0.4
2 0.2 0.0 0.5
1 0.5 0.3 0.2
1 0.4 0.2 0.1
I want to have an extra column called "my_sum" that sums the first row in all columns (W_1, W_2, W_3). The output would be something like this:
ID W_1 W_2 W_3 my_sum
1 0.1 0.2 0.3 0.6
1 0.2 0.4 0.5 1.1
2 0.3 0.3 0.2 0.8
2 0.1 0.3 0.4 0.8
2 0.2 0.0 0.5 0.7
1 0.5 0.3 0.2 1.0
1 0.4 0.2 0.1 0.7
I tred the following:
df['my_sum'] = df.groupby('ID')['W_1','W_1','W_1'].transform(sum,axis=1)
but this sums all entries of just W_1. The documentation mentions the axis parmeter, but I am not sure why it is not effective.
I looked into this question and also this, but they are different from what I want.