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

使用Python reduce函数填充DataFrame的NaN值时出错的原因咨询

Hey there! Let's break down exactly why your reduce + lambda code threw that error, and how you could fix it if you ever wanted to use reduce for this task (though your final solution is way better, by the way!).

Why the Error Happened

The core issue is how Python's reduce function works: it expects the first argument (your lambda) to accept two positional parameters, not one.

Here's what was happening with your code:

reduce(lambda x : df.fillna({x : 0}, inplace=True), ['Col_A', 'Col_B', 'Col_C', 'Col_D'])
  1. reduce starts by taking the first element in your list ('Col_A') as the initial "accumulator" value.
  2. Next, it tries to pass two values to your lambda: the accumulator ('Col_A') and the next element in the list ('Col_B').
  3. But your lambda only accepts one parameter (x), hence the error: <lambda>() takes 1 positional argument but 2 were given.

On top of that, using inplace=True makes this even worse: fillna(inplace=True) modifies your DataFrame in place and returns None. So even if you fixed the lambda parameters, after the first iteration your accumulator would become None, and subsequent calls would fail entirely.

How to Fix the reduce Approach (If You Really Wanted To)

If you were set on using reduce, you'd need to adjust the lambda to accept two parameters, and avoid inplace=True so you can pass the updated DataFrame through each iteration. Here's what that would look like:

from functools import reduce

# Define your target columns
cols = ['Col_A', 'Col_B', 'Col_C', 'Col_D']
# Use reduce with an initial value (your original df)
df = reduce(lambda acc_df, col: acc_df.fillna({col: 0}), cols, df)
  • The third argument to reduce (df) sets the initial accumulator value to your original DataFrame.
  • Each iteration passes the updated DataFrame (acc_df) and the current column name (col) to the lambda.
  • fillna returns a new DataFrame with the NaNs replaced for that column, which becomes the accumulator for the next step.
Why Your Final Solution Is Better

Your fix:

df[['Col_A', 'Col_B', 'Col_C', 'Col_D']] = df[['Col_A', 'Col_B', 'Col_C', 'Col_D']].fillna(0)

is far more idiomatic Pandas. It leverages Pandas' vectorized operations to replace NaNs across all target columns in one go, which is faster and cleaner than iterating through columns with reduce. This is exactly the kind of approach you should reach for first in Pandas.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:20:39