地狱Stackoverflow,
新手在这里:)
+-----------+-------+------+-----------+----------+---------------+
| Manager | Month | GOAL | COMPLETED | MTD_DIFF | MTD_DIFF_PERC |
+-----------+-------+------+-----------+----------+---------------+
| PERSON 1 | 2-Feb | 18 | 8 | 10 | 0.444444 |
| PERSON 2 | 1-Jan | 1 | 1 | 0 | 1 |
| PERSON 2 | 2-Feb | 14 | 10 | 4 | 0.714286 |
| PERSON 3 | 2-Feb | 15 | 17 | -2 | 1.133333 |
| PERSON 4 | 2-Feb | 10 | 8 | 2 | 0.8 |
| PERSON 5 | 1-Jan | 4 | 5 | -1 | 1.25 |
| PERSON 5 | 2-Feb | 40 | 8 | 32 | 0.2 |
| PERSON 6 | 1-Jan | 8 | 2 | 6 | 0.25 |
| PERSON 6 | 2-Feb | 5 | 7 | -2 | 1.4 |
| PERSON 7 | 1-Jan | 3 | 1 | 2 | 0.333333 |
| PERSON 7 | 2-Feb | 3 | 5 | -2 | 1.666667 |
+-----------+-------+------+-----------+----------+---------------+
然后我做一个枢轴
FINAL_DF_PIVOT = FINAL_DF.pivot_table(index = 'Manager',
columns = 'Month',
values = ['COMPLETED', 'GOAL','MTD_DIFF','MTD_DIFF_PERC'],
aggfunc={'COMPLETED': np.sum,
'GOAL': np.sum,
'MTD_DIFF': np.sum,
'MTD_DIFF_PERC':np.sum} ,
margins=True,
margins_name='YTD')
我得到的结果
+----------+-------+-----------+------+----------+---------------+
| Manager | Month | COMPLETED | GOAL | MTD_DIFF | MTD_DIFF_PERC |
+----------+-------+-----------+------+----------+---------------+
| PERSON 1 | 2-Feb | 8 | 18 | 10 | 0.444444444 |
| PERSON 1 | YTD | 8 | 18 | 10 | 0.444444444 |
| PERSON 3 | 2-Feb | 17 | 15 | -2 | 1.133333333 |
| PERSON 3 | YTD | 17 | 15 | -2 | 1.133333333 |
| PERSON 2 | 1-Jan | 1 | 1 | 0 | 1 |
| PERSON 2 | 2-Feb | 10 | 14 | 4 | 0.714285714 |
| PERSON 2 | YTD | 11 | 15 | 4 | 1.714285714 |
| PERSON 4 | 1-Jan | 1 | 3 | 2 | 0.333333333 |
| PERSON 4 | 2-Feb | 5 | 3 | -2 | 1.666666667 |
| PERSON 4 | YTD | 6 | 6 | 0 | 2 |
| PERSON 5 | 2-Feb | 8 | 10 | 2 | 0.8 |
| PERSON 5 | YTD | 8 | 10 | 2 | 0.8 |
| PERSON 6 | 1-Jan | 2 | 8 | 6 | 0.25 |
| PERSON 6 | 2-Feb | 7 | 5 | -2 | 1.4 |
| PERSON 6 | YTD | 9 | 13 | 4 | 1.65 |
| PERSON 7 | 1-Jan | 5 | 4 | -1 | 1.25 |
| PERSON 7 | 2-Feb | 8 | 40 | 32 | 0.2 |
| PERSON 7 | YTD | 13 | 44 | 31 | 1.45 |
| YTD | 1-Jan | 9 | 16 | 7 | 2.833333333 |
| YTD | 2-Feb | 63 | 105 | 42 | 6.358730159 |
| YTD | YTD | 72 | 121 | 49 | 9.192063492 |
+----------+-------+-----------+------+----------+---------------+
我不需要求和我实际上想要获得全部完成的MTD_DIFF_PERC字段
我也需要得到的结果(较小的样本)
| PERSON 2 | 1-Jan | 1 | 1 | 0 | 1 |
| PERSON 2 | 2-Feb | 10 | 14 | 4 | 0.714285714 |
| PERSON 2 | YTD | 11 | 15 | 4 | .73333333333 |
11除以15 = 73%完成。我知道需要对枢纽进行调整,只是不确定如何完成我需要的工作。
我也知道这会给我带来我需要的结果,但不确定如何将其添加到MTD_DIFF_PERC
TOTALS = FINAL_DF.groupby(['Manager'])['COMPLETED'].sum()/FINAL_DF.groupby(['Manager'])['GOAL'].sum()