DGraham DGraham - 6 months ago 14
Python Question

Insert sum of group into new column

I have a simple

DataFrame
:

gov dis value1 value2
a a_1 8 8
a a_2 7 18
a a_3 3 2
a a_4 12 12
b b_1 4 11
b b_2 16 9
b b_3 17 12
b b_4 7 9
b b_5 15 11
b b_6 13 16


I can create a sum of all values in
value1
easily.

per_t['sum_v1'] = sum(per_t['value1'])


Producing:

gov dis value1 value2 sum_v1
0 a a_1 8 8 102
1 a a_2 7 18 102
2 a a_3 3 2 102
3 a a_4 12 12 102
4 b b_1 4 11 102
5 b b_2 16 9 102
6 b b_3 17 12 102
7 b b_4 7 9 102
8 b b_5 15 11 102
9 b b_6 13 16 102


Can this be altered to calculate sum per say
gov
? I have unsuccessfully tried
groupby
and
agg
functions. If I try:

per_t['sum_gov'] = per_t.groupby('gov')['value1'].sum()


The table reads as:

gov dis value1 value2 sum_v1 sum_gov
0 a a_1 8 8 102 NaN
1 a a_2 7 18 102 NaN
2 a a_3 3 2 102 NaN
3 a a_4 12 12 102 NaN
4 b b_1 4 11 102 NaN
5 b b_2 16 9 102 NaN
6 b b_3 17 12 102 NaN
7 b b_4 7 9 102 NaN
8 b b_5 15 11 102 NaN
9 b b_6 13 16 102 NaN

Answer

You could

df['sum_gov'] = df.groupby('gov')['value1'].transform(sum)

See docs:

The transform method returns an object that is indexed the same (same size) as the one being grouped. Thus, the passed transform function should return a result that is the same size as the group chunk.

Comments