Pandas pivot table subtotals

40 views Asked by At

I have created a pandas pivot table using the below code:

pivot = df.pivot_table(index = ['Zone', 'State', 'Branch'], values = 'Loan amount', aggfunc = 'sum', fill_value = 0, margins = True, margins_name = 'Grand total')

I'm getting the grand total using the margins argument. But I couldn't find a way to get the zone wise and state wise subtotals in the pivot table. I want to display the subtotals just like in a ln Excel pivot table.

Can anyone provide the solution for this?

Appreciate your help.

Thank you.

I tried grouping based on zone and state separately and concatenate it with the pivot table. But then the index is coming in the form of a tuple. 9

0

There are 0 answers