I have a dataframe
p_id b_id val
101 201 99
101 202 2
101 203 11
102 201 1
102 202 50
102 203 23
I need to add the values in the column val, based on p_id and b_id, to get this result dataframe:
ie, for each p_id:
sum1 = val(201) + val(202) = 99 + 2 = 101
sum2 = val(201) + val(203) = 99 + 11 = 110
p_id sum_1 sum_2
101 101 110
102 51 24
The resultant dataframe should look like this
I tried running this
df2 = df.groupby(['p_id', 'b_id'])['val'].sum().reset_index()
but not being able to get all the rows for each p_id