<p>只是对@jazrael答案的更详细的描述,以及您的数据帧:</p>
<pre><code>df = pd.DataFrame({'size': list('SSMMMLS'),
'weight': [8, 10, 11, 1, 20, 14, 12],
'adult' : [False] * 5 + [True] * 2})
# adult size weight
# 0 False S 8
# 1 False S 10
# 2 False M 11
# 3 False M 1
# 4 False M 20
# 5 True L 14
# 6 True S 12
</code></pre>
<p>要获取“最大权重”行的大小值,请执行以下操作:</p>
^{pr2}$
<p>“成人”上的groupby将生成一个索引值为False和True的序列:</p>
<pre><code>>>> size2_col = df.groupby('adult').apply(size4max_weight)
>>> type(size2_col), size2_col.index
(pandas.core.series.Series, Index([False, True], dtype='object', name=u'adult'))
</code></pre>
<p>使用<code>reset_index</code>我们在DataFrame中转换serie::</p>
<pre><code>>>> size2_col = df.groupby('adult').apply(size4max_weight).reset_index(name='size2')
>>> size2_col
adult size2
0 False M
1 True L
>>>
</code></pre>
<p><code>pd.merge</code>在“成人”上:</p>
<pre><code>>>> pd.merge(df, size2_col, on=['adult'])
adult size weight size2
0 False S 8 M
1 False S 10 M
2 False M 11 M
3 False M 1 M
4 False M 20 M
5 True L 14 L
6 True S 12 L
</code></pre>