<p>我做了一个熊猫团员</p>
<pre><code>grouped = df.groupby(['name','type'])['count'].count().reset_index()
</code></pre>
<p>看起来像这样:</p>
<pre><code>name type count
x a 32
x b 1111
x c 4214
</code></pre>
<p>我需要做的是得到这个并生成百分比,所以我会得到这样的结果(我意识到百分比是不正确的):</p>
<pre><code>name type count
x a 1%
x b 49%
x c 50%
</code></pre>
<p>我可以想出一些可能有意义的伪代码,但我还没有得到任何实际工作。。。你知道吗</p>
<p>像这样的</p>
<pre><code>def getPercentage(df):
for name in df:
total = 0
where df['name'] = name:
total = total + df['count']
type_percent = (df['type'] / total) * 100
return type_percent
df.apply(getPercentage)
</code></pre>
<p>有没有一个好的方法来处理熊猫呢?你知道吗</p>