Search code examples
pythonpython-3.xpandasdataframenan

How to replace NaNs by preceding or next values in pandas DataFrame?


Suppose I have a DataFrame with some NaNs:

>>> import pandas as pd
>>> df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]])
>>> df
    0   1   2
0   1   2   3
1   4 NaN NaN
2 NaN NaN   9

What I need to do is replace every NaN with the first non-NaN value in the same column above it. It is assumed that the first row will never contain a NaN. So for the previous example the result would be

   0  1  2
0  1  2  3
1  4  2  3
2  4  2  9

I can just loop through the whole DataFrame column-by-column, element-by-element and set the values directly, but is there an easy (optimally a loop-free) way of achieving this?


Solution

  • You could use the fillna method on the DataFrame and specify the method as ffill (forward fill):

    >>> df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]])
    >>> df.fillna(method='ffill')
       0  1  2
    0  1  2  3
    1  4  2  3
    2  4  2  9
    

    This method...

    propagate[s] last valid observation forward to next valid

    To go the opposite way, there's also a bfill method.

    This method doesn't modify the DataFrame inplace - you'll need to rebind the returned DataFrame to a variable or else specify inplace=True:

    df.fillna(method='ffill', inplace=True)