简体   繁体   中英

Pandas: multiply column value by sum of group

I have a dataframe which looks like this:

   a     b
0  A  0.15
1  A  0.25
2  A  0.10
3  B  0.20
4  B  0.10
5  B  0.25
6  B  0.60
7  C  0.50
8  C  0.70

I want to add a column 'c' which multiplies the value of 'b' by the sum of the group it belongs to in column 'a'. So, first row should be 0.15 * 0.5 (sum of group A) = 0.075. This would be the excel formula for column 'c' =B1*SUMIF($A$1:$A$9,A1,$B$1:$B$9)

Resulting dataframe should look like this:

    a   b       c
0   A   0.15    0.075
1   A   0.25    0.125
2   A   0.10    0.05
3   B   0.20    0.23
4   B   0.10    0.115
5   B   0.25    0.2875
6   B   0.60    0.69
7   C   0.50    0.6
8   C   0.70    0.84

Try groupby + transform and then multiply:

df['b'] * df.groupby('a')['b'].transform('sum')
#df['c'] = df['b'] * df.groupby('a')['b'].transform('sum')

0    0.0750
1    0.1250
2    0.0500
3    0.2300
4    0.1150
5    0.2875
6    0.6900
7    0.6000
8    0.8400
Name: b, dtype: float64

Try something new reindex

df['c']=df.b*df.set_index('a').b.sum(level=0).reindex(df.a).values
df
   a     b       c
0  A  0.15  0.0750
1  A  0.25  0.1250
2  A  0.10  0.0500
3  B  0.20  0.2300
4  B  0.10  0.1150
5  B  0.25  0.2875
6  B  0.60  0.6900
7  C  0.50  0.6000
8  C  0.70  0.8400

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM