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

使用自定义函数聚合Pandas多列时遇TypeError的问题排查

Pandas分组聚合报错:TypeError: Must provide 'func' or tuples of '(column, aggfunc)'

错误原因

你的代码里,agg方法的Filtered_Mean字段使用了lambda x: arbFun(x['A'], x['B']),这不符合Pandasagg方法的命名聚合规则:

  • agg的命名聚合要求每个字段的值要么是针对单列的聚合函数,要么是(列名, 聚合函数)的元组;
  • 你写的lambda接收的是整个分组后的子DataFrame,属于多列联动的处理逻辑,这种场景下agg无法正确解析,因此抛出类型错误。

修正方案

方案1:使用groupby.apply(推荐,无需修改arbFun)

apply可以直接接收每个分组的子DataFrame,完美适配你现有arbFun的参数要求:

def process_group(group):
    return pd.Series({
        'Column_A': group['A'].sum(),
        'Filtered_Mean': arbFun(group['A'], group['B'])
    })

# 执行分组处理并重置索引
output = data.groupby(['Label1', 'Label2']).apply(process_group).reset_index()

方案2:修改arbFun适配agg

如果坚持使用agg,可以调整arbFun让它接收整个分组的DataFrame,再在agg中调用:

首先修改arbFun:

def arbFun(df):
    """
    Calculates the average value of 'A' for all rows where 'B' == 1.
    Args:
        df: 包含'A'和'B'列的Pandas DataFrame
    Returns:
        符合条件的'A'列均值,无匹配时返回None
    """
    filtered_data = df[df['B'] == 1]
    return filtered_data['A'].mean() if len(filtered_data) > 0 else None

然后使用agg执行聚合:

output = data.groupby(['Label1', 'Label2']).agg(
    Column_A=('A', 'sum'),
    Filtered_Mean=lambda x: arbFun(x)
).reset_index()

验证结果

两种方案执行后都会得到符合预期的结果(Pandas会将None显示为NaN,若需严格显示None可追加output.replace({pd.NA: None})):

Label1 Label2  Column_A  Filtered_Mean
0       1  north         2            NaN
1       1  south        18            9.0
2       2  north        10            4.0
3       2  south        12           12.0

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 04:20:34