Is there a way to make the sum in columns after grouping in a 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 result will 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 have done the following:
df['my_sum'] = df.groupby('ID')['W_1','W_1','W_1'].transform(sum,axis=1)
but this sums all the records only W_1. the documentation mentions axis transfer, but I'm not sure why it is not efficient.
I looked through this question , as well as this one , but they are different from what I want.
owise source
share