Python Pivot Table multi Sub-totals in column

Use slicers for set new values in both sides, here : means all values for levels:

idx = pd.IndexSlice
table.loc[idx[:, 'Toyota'], 'Total'] = table.max(axis=1)
table.loc[idx[:, 'Honda'], 'Total'] = table.sum(axis=1)
print (table)
                   Speed            Total
Date          13/02/2019 18/02/2019      
Target Brand                             
A      Honda          20         30  50.0
B      Toyota         10         80  80.0

You can set and select in both sides:

idx = pd.IndexSlice
table.loc[idx[:, 'Toyota'], 'Total'] = table.loc[idx[:, 'Toyota'], :].max(axis=1)
table.loc[idx[:, 'Honda'], 'Total'] = table.loc[idx[:, 'Honda'], :].sum(axis=1)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top