You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pandas中按列分组并标记组内指定列的最大值

问题描述

我有如下DataFrame:

abc
one611
one712
two823
two914
three1015
three2025

我想要对列a执行groupby操作,找出每个组内列c的最大值,并为最大值所在行添加标记,最终得到如下结果:

abcflag
one611no
one712yes
two823yes
two914no
three1015no
three2025yes

输入DataFrame的代码如下:

import pandas as pd

df = pd.DataFrame({
    'a':["one","one","two","two","three","three"],
    'b':[6,7,8,9,10,20],
    'c':[11,12,23,14,15,25]
    # , 'flag': ['no', 'yes', 'yes', 'no', 'no', 'yes']
})
解决方案

通过groupby结合transform方法获取每个组内c列的最大值,再将每行c值与组内最大值对比生成标记列:

# 计算每个分组内c列的最大值,保持与原DataFrame相同长度
max_c_per_group = df.groupby('a')['c'].transform('max')
# 生成flag列:等于最大值标记为yes,否则为no
df['flag'] = df['c'].eq(max_c_per_group).map({True: 'yes', False: 'no'})

print(df)
输出结果

执行代码后输出如下:

abcflag
one611no
one712yes
two823yes
two914no
three1015no
three2025yes

内容的提问来源于stack exchange,提问作者fast_crawler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 23:01:07