Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Panda pivot_table with only column total and no row total

Tags:

pandas

pivot

I want to have grand total for columns ONLY and not for the rows. It seems the margins flag will give me both row and column total:

pivot_operation=pd.pivot_table(df_operation,index=["Name"],values=["Hours"], columns=["Workdate"],aggfunc=[np.sum],margins=True,margins_name='Grand Total')

And I get

Hours
Workdate    02-03   02-04   02-05   02-06   02-07   Grand Total
Name                        
Joe              8.0    8.0     8.0     8.0     8.0     40.0
Mary             8.5    8.5     8.5     8.5     8.5     42.5
Grand Total     16.5    16.5    16.5    16.5    16.5    82.5

Where I only want the Columns Grand Total without Row Grand total

Hours
Workdate    02-03   02-04   02-05   02-06   02-07   Grand Total
Name                        
Joe              8.0    8.0     8.0     8.0     8.0     40.0
Mary             8.5    8.5     8.5     8.5     8.5     42.5

How can I achieve that?

like image 443
ironfugu Avatar asked Nov 21 '25 20:11

ironfugu


1 Answers

For now, I could work around this by removing the last row using iloc function() after creating the pivot with margin. aggfunc=[np.sum],margins=True,margins_name='Grand Total').iloc[:-1,:]

like image 194
ironfugu Avatar answered Nov 25 '25 00:11

ironfugu