Skip to content
Advertisement

How to replace ffill() method with custom function in pandas

here is my sample df:

JavaScript

If I would like to replace the NaN values and ffill the last number (70.2 – in this case), I would simply apply:

JavaScript

However, what if I would like to apply a custom function instead of ffill() method: For instance, I need the NaN values of y column to be replaced with “2 * x^2”. See the desired output df:

JavaScript

Just to illustrate: 2 * 7^2 = 98 etc

I appreciate any help.

Advertisement

Answer

In your case do

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement