Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

row sum on a pandas pivot table

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.

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

like image 450
spak Avatar asked Apr 05 '18 20:04

spak


1 Answers

Look at margins parameter in pivot_table:

x.pivot_table(index = ["am","spot_or_contract"], 
              columns = "status" , 
              values = "shipment_id", 
              aggfunc = "count",
              margins = True,
              fill_value = "")
like image 67
Scott Boston Avatar answered Sep 21 '22 08:09

Scott Boston