Home > OS >  Python pandas, replace a NAN on a column with previous value on the same column
Python pandas, replace a NAN on a column with previous value on the same column

Time:03-19

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.

CodePudding user response:

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

CodePudding user response:

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
  • Related