Closed mshiner closed 3 years ago
If I'm understanding correctly, don't use margins=True
. Complete your pivot, then use assign
at the end to add the row totals.
Here's a full example with the titanic data:
import pandas as pd
import sidetable
import seaborn as sns
df = sns.load_dataset('titanic')
tmp = df[['class', 'deck', 'sex', 'fare']]
pd.pivot_table(tmp, index=['class', 'deck'],
columns=['sex'],
aggfunc={'fare': 'sum'}).stb.subtotal(sub_level=[1],
grand_label='Total Fare').assign(total=lambda x: x.sum(axis=1))
Is that what you were trying to do?
Chris
Perfect!
Thank you so much
Hi Chris
Dumb question - I'd like to get the totals of columns as well as sub-totals. If I add margins=True I get a subtotal for all as well as a grand total from sidetable
M