熊猫数据透视表上的行总和

spa*_*pak 6 python pandas jupyter-notebook

我一直在试图弄清楚如何将行总和添加到 Pandas 数据透视表。请有人帮我指出正确的方向。

这是我下面的代码。

x.pivot_table(index = ["am","spot_or_contract"],
              columns = "status",
              values = "shipment_id",
              aggfunc = "count").fillna('')
Run Code Online (Sandbox Code Playgroud)

非常感谢您提前

Sco*_*ton 14

查看marginspivot_table中的参数:

x.pivot_table(index = ["am","spot_or_contract"], 
              columns = "status" , 
              values = "shipment_id", 
              aggfunc = "count",
              margins = True,
              fill_value = "")
Run Code Online (Sandbox Code Playgroud)