Skip to content
Advertisement

python pandas dataframe : fill nans with a conditional mean of previous and next value

I have the following dataframe:

JavaScript

And I want value NaN to be filled with the conditional mean of previous and next value based on the same column.

JavaScript

Just like this, value 6 is the mean with 5 and 7. And this is a little part of my dataframe, so I need to replace all the NaN.

Advertisement

Answer

EDIT:

For replace missing values in all columns use:

JavaScript

If need repalce only some columns, e.g. numeric:

JavaScript

Use:

JavaScript

JavaScript

If need means for all missing values remove mask:

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