Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

df.fillna(df.mean()) vs df.fillna(df.mean)

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks a lot!

>Solution :

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")

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading