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

查看:77
本文介绍了组中的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 + transform :

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天全站免登陆