我有一个数据框,已使用pd.pivot_table方法和求和聚合函数转换为数据透视表:
SM_waterfall = df.query('type == ["SM"] & condition == ["Carrier Damaged","Customer Damaged","Unknown"]').pivot_table(index=["shipdate"], columns=["completeddate"],aggfunc=np.sum, fill_value=0, margins=True, margins_name="Total")
amount
completeddate 2018-8 2018-9 Total
shipdate
2018-6 8430.25 0.00 8430.25
2018-7 840.24 1018.39 1858.63
2018-8 1011.10 340.82 1351.92
2018-9 0.00 2463.25 2463.25
Total 10281.59 3822.46 14104.05
amount
completeddate 2018-8 2018-9 Total
shipdate
2018-6 1.00000 0.00000 1.00000
2018-7 0.45207 0.54793 1.00000
2018-8 0.74790 0.25210 1.00000
2018-9 0.00000 1.00000 1.00000
Total 0.72898 0.27102 1.00000
我想将数据透视表中的所有值转换为最后一列的%,以使数据框显示为底部df中所示。我对此很陌生,并且看到了基于总和列而不是行进行计算的代码。