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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49680648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:25:47  来源:igfitidea点击:

row sum on a pandas pivot table

pythonpandasjupyter-notebook

提问by spak

I am stuck on trying to figure out how to add a row sum to a pandas pivot table. Would somebody please help point me towards the right direction.

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

This is my code below.

这是我下面的代码。

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

Thank you very much in advance

非常感谢您提前

回答by Scott Boston

Look at marginsparameter in pivot_table:

查看marginspivot_table中的参数:

x.pivot_table(index = ["am","spot_or_contract"], 
              columns = "status" , 
              values = "shipment_id", 
              aggfunc = "count", margins=True)\
 .fillna('')