Skip to content

Instantly share code, notes, and snippets.

@hute37
Forked from aniruddha27/Pivot_table_margins.py
Created June 15, 2020 09:05
Show Gist options
  • Save hute37/09ff79aeb03a419a5d250eb2aa947e6b to your computer and use it in GitHub Desktop.
Save hute37/09ff79aeb03a419a5d250eb2aa947e6b to your computer and use it in GitHub Desktop.
#displays the subtotal using margins and margins_name
# margins is a boolean field that should be made True if total values need to be printed.
# margins_name is the optional field where you can name the margin row/column in your grouped table.
table = pd.pivot_table(df,index=['Sex'],columns=['Pclass'],values=['Survived'],aggfunc=np.sum, margins=True, margins_name='Total_Survived')
table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment