擅长:python、mysql、java
<p>使用<a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html#transformation" rel="nofollow">^{<cd1>}</a>从<code>groupby</code>聚合中添加一列,这将创建一个<code>Series</code>,其索引与原始df对齐,以便您可以正确地重新分配它:</p>
<pre><code>In [4]:
df['meanconcentration'] = df.groupby('roundtime')['Concentration'].transform('mean')
df
Out[4]:
Studynumber Time Concentration roundtime meanconcentration
0 1 20 80 20 87.5
1 1 40 60 40 65.0
2 1 60 40 60 35.0
3 2 15 95 20 87.5
4 2 44 70 40 65.0
5 2 65 30 60 35.0
</code></pre>