Maximum Python Pandas value in group as new column

I am trying to compute a new column that contains the maximum values ​​for each of several groups. I am coming from the Stata background, so I know that the Stata code will be something like this:

by group, sort: egen max = max(odds) 

For instance:

data = {'group' : ['A', 'A', 'B','B'],
    'odds' : [85, 75, 60, 65]}

Then I would like it to look like this:

    group    odds    max
     A        85      85
     A        75      85
     B        60      65
     B        65      65

In the end, I try to form a column that accepts 1/(max-min) * oddswhere maxand minfor each group.

+5
source share
2 answers

Use + : groupby transform

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

This is equivalent to verbose:

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

transform groupby groupby , .

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

All Articles