I have the following data:
Employee Account Currency Amount Location
Test 2 Basic USD 3000 Airport
Test 2 Net USD 2000 Airport
Test 1 Basic USD 4000 Town
Test 1 Net USD 3000 Town
Test 3 Basic GBP 5000 Town
Test 3 Net GBP 4000 Town
I can manage to pivot by doing the following:
import pandas as pd
table = pd.pivot_table(df, values=['Amount'], index=['Location', 'Employee'], columns=['Account', 'Currency'], fill_value=0, aggfunc=np.sum, dropna=True)
Output:
Amount
Account Basic Net
Currency GBP USD GBP USD
Location Employee
Airport Test 2 0 3000 0 2000
Town Test 1 0 4000 0 3000
Test 3 5000 0 4000 0
How can I achieve subtotal by location and then a final grand total at the bottom.
Desired output:
Amount
Account Basic Net
Currency GBP USD GBP USD
Location Employee
Airport Test 2 0 3000 0 2000
Airport Total 3000 0 2000
Town Test 1 0 4000 0 3000
Test 3 5000 0 4000 0
Town Total 5000 4000 4000 3000
Grand Total 5000 7000 4000 5000
I tried following the following. But it does not give the desired output.
Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…