'Python pandas, replace a NAN on a column with previous value on the same column

I am working with pandas DF

0                   2.143                2.503         4.977  
1                   2.148                2.508         4.977  
2                     NaN                  NaN          4.90  
3                   2.150                2.511         4.979  
4                     NaN                2.489           NaN  
5                   2.148                2.509         4.977  

I would like to replace the NaN with the previous row value on the same column, as shown below

0                   2.143                2.503         4.977  
1                   2.148                2.508         4.977  
2                   2.148                2.508         4.90   
3                   2.150                2.511         4.979  
4                   2.150                2.489         4.979   
5                   2.148                2.509         4.977 

I am not sure how to do it replace nan with previous row value.



Solution 1:[1]

You can use .fillna() with method='ffill'.

import pandas as pd
from numpy import nan

df = pd.DataFrame({1: [2.143, 2.148, nan, 2.15, nan, 2.148],
                   2: [2.503, 2.508, nan, 2.511, 2.489, 2.509],
                   3: [4.977, 4.977, 4.9, 4.979, nan, 4.977]})
>>> df
       1      2      3
0  2.143  2.503  4.977
1  2.148  2.508  4.977
2    NaN    NaN  4.900
3  2.150  2.511  4.979
4    NaN  2.489    NaN
5  2.148  2.509  4.977

>>> df.fillna(method='ffill')
       1      2      3
0  2.143  2.503  4.977
1  2.148  2.508  4.977
2  2.148  2.508  4.900
3  2.150  2.511  4.979
4  2.150  2.489  4.979
5  2.148  2.509  4.977

Solution 2:[2]

Use pd.DataFrame.ffill:

df.ffill()

Using @fsimonjetz setup:

import pandas as pd
from numpy import nan

df = pd.DataFrame({1: [2.143, 2.148, nan, 2.15, nan, 2.148],
                   2: [2.503, 2.508, nan, 2.511, 2.489, 2.509],
                   3: [4.977, 4.977, 4.9, 4.979, nan, 4.977]})

>>> df
       1      2      3
0  2.143  2.503  4.977
1  2.148  2.508  4.977
2    NaN    NaN  4.900
3  2.150  2.511  4.979
4    NaN  2.489    NaN
5  2.148  2.509  4.977

>>> df.ffill()

       1      2      3
0  2.143  2.503  4.977
1  2.148  2.508  4.977
2  2.148  2.508  4.900
3  2.150  2.511  4.979
4  2.150  2.489  4.979
5  2.148  2.509  4.977

Solution 3:[3]

thank you everyone. Exactly what i wanted to do. When i did not assigned to new dataframe and just did df.ffill(), the NAN were not forward filled but if i assign to new dataframe, it does. did i miss anything.

import pandas as pd
import numpy as np


# load soure file 
df=pd.read_excel("ABC.xls")
print(df)

#df.fillna(method='ffill')
df1=df.ffill()   # unless assigned to df1 it was not replacing NAN
print(df1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Scott Boston
Solution 3 Dev D