I am trying to add subtotal rows in a pivot table (using pandas pd.pivot_table). This is the code table = pd.pivot_table(df, values= ['Quantity', 'Money', 'Cost'], index=['house','date', 'currency', 'family name'], columns=[], fill_value=0, aggfunc=np.sum)
. This is the corresponding output (exported to excel):
Then, I tried to get a subtotal row using house
as a reference. I followed the steps stated in this link Pivot table subtotals in Pandas so I created a group using tablesum = table.groupby(level='house').sum()
. everything seemed to be fine until I tried to concatenate table
and tablesum
dataframes. This is what I got (for family A):
Basically, I obtained the four categories stated in table´s index (house, date, currency, family name) in just one column (separate by commas). So, even when I get the subtotal by house, I lost the pivot_table separation. So, my question is: how can I keep it (mantaining pivot_table´s index in different columns)?
Any help would be highly apprecited it.
Regards,
pd: I also checked this link Sub Total in pandas pivot Table but this gave me another type of error related to strings and numbers.