I would like to find an easy way to compute the % of each sub-category in the category after i do the sum for each group. Here is an example:
df = pd.DataFrame({'key': ['A', 'B', 'C', 'A', 'B', 'C'],
'data1': range(6),
'data2': ['A1', 'B1', 'C1', 'A2', 'B2', 'C2']},
columns = ['key', 'data1', 'data2'])
df.groupby(['key','data2'])['data1'].sum()
What I would like to do is create an additional column which show the % of each sub-category (i.e., A1 etc) in the respective category (i.e, A etc). For example I would like to know the percentages of A1/sum(A1+A2) till C2/sum(C1+C2).
Whats the easiest way to do that please?