Home > Software engineering >  df.fillna(df.mean()) vs df.fillna(df.mean)
df.fillna(df.mean()) vs df.fillna(df.mean)

Time:12-28

I'm puzzled by this. To fillna with mean, I should use df.fillna(df.mean()) but I made the mistake of dropping the bracket df.fillna(df.mean)

I got a totally different result. The documentation for fillna does accept a scalar, dict, Series, or DataFrame but when I type: type(df.mean) I got a method as type which does not make any sense. It's not any of the scalar, dict, Series, or DataFrame accepted by fillna.

What is actually happening here? I hope somebody could help enlighten me.

Thanks a lot!

CodePudding user response:

I believe the method itself is used as a fill value, as a scalar:

df = pd.DataFrame({'a':[1, np.nan]})
df.fillna(df.mean)

so no surprise here:

    a
0   1
1   <bound method DataFrame.mean of a\n0 1.0...

it is like you do df.fillna(lambda x: "some func")

  • Related