1 回答
TA贡献1765条经验 获得超5个赞
尝试transform
df['PCT']=df['Costs']/df.groupby('Name')['Costs'].transform('sum')
df
Out[98]:
Name Month Costs PCT
0 Painting 1 1000 0.25
1 Painting 2 0 0.00
2 Painting 3 0 0.00
3 Painting 4 0 0.00
4 Painting 5 1000 0.25
5 Painting 6 0 0.00
6 Painting 7 0 0.00
7 Painting 8 0 0.00
8 Painting 9 1000 0.25
9 Painting 10 0 0.00
10 Painting 11 0 0.00
11 Painting 12 1000 0.25
12 Repair 1 0 0.00
13 Repair 2 0 0.00
14 Repair 3 0 0.00
15 Repair 4 2500 0.25
16 Repair 5 0 0.00
17 Repair 6 0 0.00
18 Repair 7 5000 0.50
19 Repair 8 0 0.00
20 Repair 9 0 0.00
21 Repair 10 2500 0.25
22 Repair 11 0 0.00
23 Repair 12 0 0.00
添加回答
举报