Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas assign the groupby sum value to the last row in the original table

For example, I have a table

A

id  price sum
1     2    0
1     6    0
1     4    0
2     2    0
2     10   0
2     1    0
2     5    0
3     1    0
3     5    0

What I want is like (the last row of sum should be the sum of price of a group)

id  price sum
1     2    0
1     6    0
1     4    12
2     2    0
2     10   0
2     1    0
2     5    18
3     1    0
3     5    6

What I can do is find out the sum using

A['price'].groupby(A['id']).transform('sum')

However I don't know how to assign this to the sum column (last row).

Thanks

like image 688
Kevin Avatar asked Jan 03 '23 18:01

Kevin


1 Answers

Use last_valid_index to locate rows to fill

g = df.groupby('id')
l = pd.DataFrame.last_valid_index
df.loc[g.apply(l), 'sum'] = g.price.sum().values
df

   id  price  sum
0   1      2    0
1   1      6    0
2   1      4   12
3   2      2    0
4   2     10    0
5   2      1    0
6   2      5   18
7   3      1    0
8   3      5    6
like image 81
piRSquared Avatar answered Jan 08 '23 12:01

piRSquared