Python Pandas max value in a group as a new column Python Pandas max value in a group as a new column pandas pandas

Python Pandas max value in a group as a new column


Use groupby + transform:

df['max'] = df.groupby('group')['odds'].transform('max')

This is equivalent to the verbose:

maxima = df.groupby('group')['odds'].max()df['max'] = df['group'].map(maxima)

The transform method aligns the groupby result to the groupby indexer, so no explicit mapping is required.


Using the approach from jpp above works, but it also gives a "SettingWithCopyWarning". While this may not be an issue, I believe the code below would remove that warning:

df = df.assign(max = df.groupby('group')['odds'].transform('max')).values


df['max'] = df.group_col.map(lambda x: df.groupby('group_col').odds.max()[x])