I have the following code which works perfectly putting in subtotals and grand totals. With the frame.append method deprecated how should this be rewritten?
pvt = pd.concat([y.append(y.sum()
.rename((x, 'Total')))
for x, y in table.groupby(level=0)
]).append(table.sum()
.rename(('Grand', 'Total')))
Prior to this, I created a pivot table. So I'm looking for the totals to be stacked, not added as another column
pivot = pd.pivot_table(data=df2,
index=['date_created','BuyerName'],
aggfunc='sum').round()
I get the following error with suggestion #2
---> 17 pvt = pd.concat([x for _, y in table.groupby(level=0) for x in (y, y.sum().rename((x, 'Total')))] +
18 [table.sum().rename(('Grand', 'Total'))])
'Total')))
25 return(pvt)
UnboundLocalError: local variable 'x' referenced before assignment