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?

3
  • 3
    are those empty rows? if they are, you can convert to null values (np.nan) and downward fill using fillna Commented Mar 13, 2020 at 5:38
  • it is not empty row. the column value is blank. Commented Mar 13, 2020 at 5:47
  • 2
    Does this answer your question? Pandas(Python) : Fill empty cells with with previous row value? Commented Mar 13, 2020 at 5:48

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.