# Python Pandas Groupby按类别占总数的百分比

``````+-----+----------+---+
| Grp | Category | X |
+-----+----------+---+
|   1 | A        | 1 |
|   1 | B        | 3 |
|   1 | B        | 2 |
|   1 | C        | 2 |
|   2 | A        | 2 |
|   2 | A        | 4 |
|   2 | B        | 4 |
|   3 | A        | 3 |
|   3 | C        | 7 |
+-----+----------+---+
``````

``````+-----+----------+---------+
| Grp | Category | X_ratio |
+-----+----------+---------+
|   1 | A        | 1/8     |
|   1 | B        | 5/8     |
|   1 | C        | 2/8     |
|   2 | A        | 6/10    |
|   2 | B        | 4/10    |
|   3 | A        | 3/10    |
|   3 | C        | 7/10    |
+-----+----------+---------+
``````

det

Because performance is important first aggregate `sum` to `MultiIndex Series` and then divide by `Series.div` summed values per first `Grp` level:

``````s = df.groupby(['Grp','Category'])['X'].sum()
df = s.div(s.sum(level=0), level=0).reset_index(name='X_ratio')
print (df)
Grp Category  X_ratio
0    1        A    0.125
1    1        B    0.625
2    1        C    0.250
3    2        A    0.600
4    2        B    0.400
5    3        A    0.300
6    3        C    0.700
``````

``````df = (df.groupby(['Grp','Category'])['X'].sum()
.groupby(level=0)
.apply(lambda x: x / x.sum())
.reset_index(name='X_ratio'))
print (df)
Grp Category  X_ratio
0    1        A    0.125
1    1        B    0.625
2    1        C    0.250
3    2        A    0.600
4    2        B    0.400
5    3        A    0.300
6    3        C    0.700
``````