Python Pandas 组中的最大值作为新列 [英] Python Pandas max value in a group as a new column

查看:58
本文介绍了Python Pandas 组中的最大值作为新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算一个新列,其中包含多个组中每个组的最大值.我来自 Stata 背景,所以我知道 Stata 代码将是这样的:

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 something like this:

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

例如:

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

然后我希望它看起来像:

Then I would like it to look like:

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

最终我试图形成一个列,它需要 1/(max-min) *odds 其中 maxmin 是每个团体.

Eventually I am trying to form a column that takes 1/(max-min) * odds where max and min are for each group.

推荐答案

使用 groupby + 转换:

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

这相当于冗长:

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

transform 方法将 groupby 结果与 groupby 索引器对齐,因此不需要显式映射.

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

这篇关于Python Pandas 组中的最大值作为新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆