How can I easily and subtotals for each level and express the numbers in percentages in Python? I have the data below and I would like to add one column which will express the numbers as the percentage of total values in column or as a percentage of total values for a given level. How can I do that easily?
import pandas as pd
df=pd.DataFrame({'A':['x','y','z','x','y','z'],
'B':['one','one','one','two','two','two'],
'C':[2,18,2,8,2,18]})
df
table = pd.pivot_table(df, index=['A', 'B'],aggfunc=np.sum)
Is there any other solution than the one proposed here Pandas pivot table Percent Calculations?