查看每月创建了多少任务以及完成了多少任务。
我想在多索引表中添加一行,以显示每种任务类型的任务完成百分比。
因此,对于每个任务(A,B,C,D,E),我希望有“总计”,“完成”和“已完成百分比”行(完成行除以总计)
努力解决这个问题,感谢您的帮助!
当前表:
Month June July August September
Tasks Des
A Total 181.0 85.0 69.0 15.0
Complete 33.0 10.0 0.0 0.0
B Total 13.0 12.0 5.0 1.0
Complete 5.0 9.0 0.0 1.0
C Total 137.0 89.0 78.0 22.0
Complete 66.0 54.0 27.0 12.0
D Total 629.0 203.0 174.0 51.0
Complete 451.0 127.0 87.0 28.0
E Total 135.0 100.0 86.0 24.0
Complete 46.0 27.0 29.0 2.0
答案 0 :(得分:0)
使用unstack
进行整形,因此可以按行划分,最后通过stack
重新整形。
最后swaplevel
,sort_index
和最后reindex
正确排序。
df1 = df.unstack(0)
df1.loc['% Completed', :] = df1.loc['Complete', :] / df1.loc['Total', :]
vals = ['Complete','Total','% Completed']
df1 = df1.stack().swaplevel(0,1).sort_index().reindex(vals, level=1)
print (df1)
June July August September
Tasks Des
A Complete 33.000000 10.000000 0.000000 0.000000
Total 181.000000 85.000000 69.000000 15.000000
% Completed 0.182320 0.117647 0.000000 0.000000
B Complete 5.000000 9.000000 0.000000 1.000000
Total 13.000000 12.000000 5.000000 1.000000
% Completed 0.384615 0.750000 0.000000 1.000000
C Complete 66.000000 54.000000 27.000000 12.000000
Total 137.000000 89.000000 78.000000 22.000000
% Completed 0.481752 0.606742 0.346154 0.545455
D Complete 451.000000 127.000000 87.000000 28.000000
Total 629.000000 203.000000 174.000000 51.000000
% Completed 0.717011 0.625616 0.500000 0.549020
E Complete 46.000000 27.000000 29.000000 2.000000
Total 135.000000 100.000000 86.000000 24.000000
% Completed 0.340741 0.270000 0.337209 0.083333
通过DataFrame.xs
进行选择,通过assign
和set_index
划分和创建MultiIndex
级别的另一个解决方案:
df1 = (df.xs('Complete', level=1)
.div(df.xs('Total', level=1))
.assign(Des='% Completed')
.set_index('Des', append=True)
)
vals = ['Complete','Total','% Completed']
df1 = df.append(df1).sort_index().reindex(vals, level=1)