I know this solution How to make a pandas crosstab with percentages?, but the solution proposed does not work with three-way tables.
Consider the following table:
df = pd.DataFrame({'A' : ['one', 'one', 'two', 'three'] * 6,
'B' : ['A', 'B', 'C'] * 8,
'C' : ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 4})
pd.crosstab(df.A,[df.B,df.C],colnames=['topgroup','bottomgroup'])
Out[89]:
topgroup A B C
bottomgroup bar foo bar foo bar foo
A
one 2 2 2 2 2 2
three 2 0 0 2 2 0
two 0 2 2 0 0 2
Here, I would like to get the row percentage, within each topgroup (A, B and C).
Using apply(lambda x: x/sum(),axis=1)
will fail because percentages have to sum to 1 within each group.
Any ideas?