groupby+sum/mean/ect then have the grouped by values go back to the original-ungrouped indexes in the original dataframe?

Viewed 86

I am grouping by values and then merging back into the original table later. I was wondering if there was any way to avoid doing this.

Like I have a table

a b v
A A 9
A B 3
A A 2
B B 4
B B 3

I want to get:

a b v
A A 11
A B 3
A A 11
B B 7
B B 7

where the new v is the sumed value of old v when grouped by a and b, w/o having unique pairs disappear after being grouped.

right now I am grouping and then joining with code that looks like this:

test = df.groupby([a,b]).sum()
test.name = new_name
df.join(test, on = [a,b], how = 'left')

Which seems a little contrived, and I was wondering if there was a way to avoid even having to join.

1 Answers

Try with transform

df['v']=df.groupby(['a','b']).v.transform('sum')
df
   a  b   v
0  A  A  11
1  A  B   3
2  A  A  11
3  B  B   7
4  B  B   7
Related