Search code examples
pythonpandasrowmean

How to impute missing values with mean from row above and below python?


I have a pandas df looking like this: df image

The row I want to impute values for has "-" instead of values and I want these to be replaced with the mean of the values in the row above and below.

Does anyone know how to do that?

Thank you all!


Solution

  • You can replace "-" to NaN and use interpolate which by default fills missing values linearly. If there is only one missing value, then it would be akin to taking the mean of the top and bottom value of the missing value:

    df = df.replace('-', np.nan)
    df = df.interpolate()