Just a more verbose version of @ jazrael's answer, with your data framework:
df = pd.DataFrame({'size': list('SSMMMLS'),
'weight': [8, 10, 11, 1, 20, 14, 12],
'adult' : [False] * 5 + [True] * 2})
To get the size value for the maximum weight line:
def size4max_weight(subf):
""" Return size value for the max weight line """
return subf['size'][subf['weight'].idxmax()]
A group with an "adult" will create a series with False, True as index values:
>>> 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'))
With reset_indexwe convert the series to a DataFrame ::
>>> size2_col = df.groupby('adult').apply(size4max_weight).reset_index(name='size2')
>>> size2_col
adult size2
0 False M
1 True L
>>>
pd.merge on 'adult' do this:
>>> 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
source
share