<p>我会这样做:</p>
<pre><code>df1=df.groupby('col_to_group')['col_1','col_2'].apply(lambda x: x.apply(lambda x: x.value_counts(normalize=True),axis=0)).unstack(level=1).dropna(how='all',axis=1).fillna(0)
print(df1)
</code></pre>
<hr/>
<pre><code> col_1 col_2
a b c x y
col_to_group
A 0.666667 0.333333 0.0 0.666667 0.333333
B 0.500000 0.000000 0.5 0.000000 1.000000
</code></pre>
<hr/>
<pre><code>df2=df[['col_1','col_2']].apply(lambda x: x.value_counts(normalize=True)).unstack().dropna().rename('ALL').to_frame().T
print(df2)
col_1 col_2
a b c x y
ALL 0.6 0.2 0.2 0.4 0.6
</code></pre>
<hr/>
<pre><code>plot_df=pd.concat([df1,df2])
print(plot_df)
col_1 col_2
a b c x y
A 0.666667 0.333333 0.0 0.666667 0.333333
B 0.500000 0.000000 0.5 0.000000 1.000000
ALL 0.600000 0.200000 0.2 0.400000 0.600000
</code></pre>
<hr/>
<pre><code>plot_df['col_1'].plot(kind='bar',stacked=True)
plot_df['col_2'].plot(kind='bar',stacked=True)
</code></pre>
<p><a href="https://i.stack.imgur.com/4l7nR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4l7nR.png" alt="enter image description here"/></a></p>
<p><a href="https://i.stack.imgur.com/rs9uz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rs9uz.png" alt="enter image description here"/></a></p>