12

Can I have a custom function for fill na in Pandas?

s.fillna(lambda r: r["SomeColumn"]**2)

I'd love to have a custom function that could take in the entire row itself.

  • how about using transform? like here https://stackoverflow.com/questions/36191286/pandas-fill-na-with-group-value/36191432#36191432 – dmb Mar 25 '16 at 20:07

1 Answers1

16

The value argument can be a Series or DataFrame. So you could calculate the correct value ahead of time, and pass that to .fillna

fill_value = s['SomeColumn'] ** 2
s.fillna(fill_value)
TomAugspurger
  • 28,234
  • 8
  • 86
  • 69