I have the following Pandas Series:
Count
Pclass Survived
1 0 80
1 136
2 0 97
1 87
3 0 372
1 119
But I want something like this:
Count Percentage
Pclass Survived
1 0 80 37.0
1 136 63.0
2 0 97 52.7
1 87 47.3
3 0 372 75.8
1 119 24.2
I want to calculate the percentages based on only the counts in each category of Pclass (not the whole sum of counts). It would be great if these percentages are calculated using only the Count column.
So far, what I did was I summed up the counts according to Pclass and used .repeat() to duplicate the values and tried to concatenate it to the original Series, which failed spectacularly.
Count
Pclass
1 216
1 216
2 184
2 184
3 491
3 491
My idea was to use this repeat column as the denominators in calculating the percentages, like this:
80 / 216 * 100 = 37.0%
and then remove the repeat column after the percentages are calculated. Seems so simple but I can't seem to make it work. Any help is appreciated.