计算n行中总和的百分比

时间:2020-09-13 20:34:20

标签: python pandas

我有一个如下所示的df。它具有每个月1-12的费用。我想用每12行(第1-12个月)的“费用”总和的百分比创建一个新列。因此,它将花费12行的总和,然后从“成本”中的值计算百分比。 我真的不知道从哪里开始。感谢您的帮助!

输入:

| Name     | Month  | Costs |
|----------|--------|-------|
| Painting | 1      | 1000  |
| Painting | 2      | 0     |
| Painting | 3      | 0     |
| Painting | 4      | 0     |
| Painting | 5      | 1000  |
| Painting | 6      | 0     |
| Painting | 7      | 0     |
| Painting | 8      | 0     |
| Painting | 9      | 1000  |
| Painting | 10     | 0     |
| Painting | 11     | 0     |
| Painting | 12     | 1000  |
| Repair   | 1      | 0     |
| Repair   | 2      | 0     |
| Repair   | 3      | 0     |
| Repair   | 4      | 2500  |
| Repair   | 5      | 0     |
| Repair   | 6      | 0     |
| Repair   | 7      | 5000  |
| Repair   | 8      | 0     |
| Repair   | 9      | 0     |
| Repair   | 10     | 2500  |
| Repair   | 11     | 0     |
| Repair   | 12     | 0     |

想要的输出:

| Name     | Period | Costs | Percentages |
|----------|--------|-------|-------------|
| Painting | 1      | 1000  | 25%         |
| Painting | 2      | 0     | 0%          |
| Painting | 3      | 0     | 0%          |
| Painting | 4      | 0     | 0%          |
| Painting | 5      | 1000  | 25%         |
| Painting | 6      | 0     | 0%          |
| Painting | 7      | 0     | 0%          |
| Painting | 8      | 0     | 0%          |
| Painting | 9      | 1000  | 25%         |
| Painting | 10     | 0     | 0%          |
| Painting | 11     | 0     | 0%          |
| Painting | 12     | 1000  | 25%         |
| Repair   | 1      | 0     | 0%          |
| Repair   | 2      | 0     | 0%          |
| Repair   | 3      | 0     | 0%          |
| Repair   | 4      | 2500  | 25%         |
| Repair   | 5      | 0     | 0%          |
| Repair   | 6      | 0     | 0%          |
| Repair   | 7      | 5000  | 50%         |
| Repair   | 8      | 0     | 0%          |
| Repair   | 9      | 0     | 0%          |
| Repair   | 10     | 2500  | 25%         |
| Repair   | 11     | 0     | 0%          |
| Repair   | 12     | 0     | 0%          |

1 个答案:

答案 0 :(得分: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