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

如何用原生Pandas为分组后的每行添加组级计算值?

问题

现有如下Pandas DataFrame:

#  name            abbr    country
0   454 Liverpool       UCL     England
1   454 Bayern Munich   UCL     Germany
2   223 Manchester United UEL   England
3   454 Manchester City  UCL     England

已通过groupby()分组并为每组计算出单个competition值,代码如下:

def test_func(abbreviation):
    if abbreviation == 'UCL':
        return 'UEFA Champions League'
    elif abbreviation == 'UEL':
        return 'UEFA Europe League'

data = [[454, 'Liverpool', 'UCL', 'England'], 
        [454, 'Bayern Munich', 'UCL', 'Germany'], 
        [223, 'Manchester United', 'UEL', 'England'], 
        [454, 'Manchester City', 'UCL', 'England']]
df = pd.DataFrame(data, columns=['#','name','abbr', 'country'])
competition_df = df.groupby('#').first()
competition_df['competition'] = competition_df.apply(lambda row: test_func(row["abbr"]), axis=1)

需求是将该competition值添加到原DataFrame对应分组的所有行中,期望结果如下:

#   name            abbr    country  competition
0   454 Liverpool       UCL     England  UEFA Champions League
1   454 Bayern Munich   UCL     Germany  UEFA Champions League
2   223 Manchester United UEL   England  UEFA Europe League
3   454 Manchester City  UCL     England  UEFA Champions League

目前已通过循环实现但效率较低,寻求使用原生Pandas方法的高效解决方案,避免迭代和列表操作。

高效解决方案

方法1:利用merge合并数据

直接将计算好competition值的competition_df与原df按#列合并,自动填充对应分组的competition值:

# 重置competition_df的索引,让#变回普通列
competition_df = competition_df.reset_index()
# 合并原df和competition_df,仅保留需要的关联列
result_df = df.merge(competition_df[['#', 'competition']], on='#', how='left')

方法2:使用groupby.transform直接生成列

无需单独生成competition_df,直接在原DataFrame上通过transform为每组生成重复的competition值:

def get_competition(group):
    # 取组内第一个abbr值转换为赛事全称
    abbr = group['abbr'].iloc[0]
    return test_func(abbr)

df['competition'] = df.groupby('#')['abbr'].transform(get_competition)

方法3:字典映射(最优方案)

观察到同一#分组的abbr值一致,且competition仅由abbr决定,直接用字典映射abbr列即可,完全跳过分组操作,效率最高:

# 定义abbr到赛事全称的映射字典
competition_map = {
    'UCL': 'UEFA Champions League',
    'UEL': 'UEFA Europe League'
}
# 直接生成competition列
df['competition'] = df['abbr'].map(competition_map)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 13:45:28