ironfugu
ironfugu

Reputation: 71

Panda pivot_table with only column total and no row total

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?

Upvotes: 3

Views: 3480

Answers (1)

ironfugu
ironfugu

Reputation: 71

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,:]

Upvotes: 4

Related Questions