擅长:python、mysql、java
<p>您可以使用带有3列的<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.pivot_table.html" rel="nofollow">^{<cd1>}</a>作为参数<code>values</code>。<code>Aggfunc=[np.mean]</code>可以省略,因为这是默认的聚合函数。最后,如果需要输出为<code>numpy array</code>,请使用<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.values.html" rel="nofollow">^{<cd2>}</a>:</p>
<pre><code>print (pd.pivot_table(dfout3, index=['Idx'], values=['Col1', 'Col2', 'Col3']))
</code></pre>
<p>样品:</p>
^{pr2}$
<pre><code>print (MeanTable1)
mean
Col1
Idx
1 2.0
2 3.5
3 3.5
print (MeanTable2)
mean
Col2
Idx
1 6
2 3
3 4
print (MeanTable3)
mean
Col3
Idx
1 3.0
2 7.0
3 7.5
print (pd.pivot_table(dfout3, index=['Idx'], values=['Col1', 'Col2', 'Col3']))
Col1 Col2 Col3
Idx
1 2.0 6.0 3.0
2 3.5 3.0 7.0
3 3.5 4.0 7.5
print (pd.pivot_table(dfout3, index=['Idx'], values=['Col1', 'Col2', 'Col3']).values)
[[ 2. 6. 3. ]
[ 3.5 3. 7. ]
[ 3.5 4. 7.5]]
</code></pre>