Python Pandas max value in a group as a new column

后端 未结 3 1666
温柔的废话
温柔的废话 2020-12-07 00:51

I am trying to calculate a new column which contains maximum values for each of several groups. I\'m coming from a Stata background so I know the Stata code would be somethi

相关标签:
3条回答
  • 2020-12-07 01:27

    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
    
    0 讨论(0)
  • 2020-12-07 01:28

    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.

    0 讨论(0)
  • 2020-12-07 01:40
    df['max'] = df.group_col.map(lambda x: df.groupby('group_col').odds.max()[x])
    
    0 讨论(0)
提交回复
热议问题