擅长:python、mysql、java
<p>您可以按分组、计算平均值和透视结果:</p>
<pre><code>df.groupby(['Variety', 'Reviewer']).mean() \
.reset_index() \
.pivot(index='Reviewer', columns='Variety')
Score
Variety Cabernet Pinot
Reviewer
Bill 88.5 86.5
Sally 84.5 89.0
</code></pre>
<p>第一行计算平均值,最后一行适当地重塑数据帧。第二行是获得<code>.mean()</code>的输出的必要条件,它的形状适合<code>pivot()</code>函数。你知道吗</p>
<p><strong>一步一步地</strong>:</p>
<pre><code>df.groupby(['Variety', 'Reviewer']).mean()
Score
Variety Reviewer
Cabernet Bill 88.5
Sally 84.5
Pinot Bill 86.5
Sally 89.0
df.groupby(['Variety', 'Reviewer']).mean().reset_index()
Variety Reviewer Score
0 Cabernet Bill 88.5
1 Cabernet Sally 84.5
2 Pinot Bill 86.5
3 Pinot Sally 89.0
</code></pre>
<p>我们现在可以使用<code>pd.pivot()</code>将其从“长格式”转换为“宽格式”,并在本文顶部获得结果。你知道吗</p>