I have a data frame:
product = DataFrame({'_product': ['shoes','dress','cap','shoes','purse','t-shirt','t-shirt','dress','t-shirt'],
'city': ['A','A','A','B','A','A','B','C','A'],
'color':['red','black','black','white','black','green','white','yellow','blue'],
'size':['36','S','M','40','-','L','L','M','S'],
'param1':['x0001','x0008','x0006','x0002','x0001','x0009','x0011','x0003','x0001'],
'param2':[23,1,367,689,35,97,100,44,15],
'param3':['f1','t1','u7','f1','r4','f2','f2','t2','f4'],
'counter':[1,1,1,1,1,1,1,1,1]})
table=product[['_product','city','color','size','param1','param2','param3','counter']]
Applying
pivot_product=pivot_table(table,values=['counter'],rows=['_product','city','color','size','param1','param2','param3'],aggfunc=[np.sum],fill_value=0,margins=True)
I get a pivot table with Grand Total row only ("All").
This is a hypothetical sample, in reality I import a table with 100 000 rows and 20 columns.
!! It is absolutely necessary for me to have subtotals on the product level.
Is there any efficient way to insert rows with subtotals into this table just as Excel pivot table with Field Setting>Layout & Print>"Show item labels in tabular form" allows to do?