2

This is my pandas dataframe.

                     Status   
index                                    
2011-01-10 16:00:00  Active
2011-01-11 16:00:00  
2011-01-12 16:00:00  Inactive
2011-01-13 16:00:00  
2011-01-14 16:00:00     
2011-01-18 16:00:00     

I would like to fill the blank value of the column 'Status' using previous N row value. The result look like following.

                     Status   
index                                    
2011-01-10 16:00:00  Active
2011-01-11 16:00:00  Active
2011-01-12 16:00:00  Inactive
2011-01-13 16:00:00  Inactive
2011-01-14 16:00:00  Inactive   
2011-01-18 16:00:00  Inactive

What's the best way to do?

matrixxx
  • 33
  • 5
  • 3
    are those empty rows? if they are, you can convert to null values (np.nan) and downward fill using fillna – sammywemmy Mar 13 '20 at 05:38
  • it is not empty row. the column value is blank. – matrixxx Mar 13 '20 at 05:47
  • 2
    Does this answer your question? [Pandas(Python) : Fill empty cells with with previous row value?](https://stackoverflow.com/questions/41212273/pandaspython-fill-empty-cells-with-with-previous-row-value) – Shaido Mar 13 '20 at 05:48

1 Answers1

3

if the rows are already null, then forward fill should be sufficient:

  df.ffill()

if the rows are empty string, then replace with np.nan, and forward fill:

  df.replace('',np.nan).ffill()


                       status
 index  
2011-01-10 16:00:00    Active
2011-01-11 16:00:00    Active
2011-01-12 16:00:00    Inactive
2011-01-13 16:00:00    Inactive
2011-01-14 16:00:00    Inactive
2011-01-18 16:00:00    Inactive
sammywemmy
  • 27,093
  • 4
  • 17
  • 31