2

I have the following data set.

I have the following data set. As you see some of the values in column A, B, D, and R are missing. I am going to replace the values with exact values from previous rows. for example, in row 4, the value for column B and R are missing, and I need to replace them with 21 and 2 from pervious column.

  A   B        D        R        sentence              ADR1         ADR2     
 135 21  EffexorXR.21    1    lack of good feeling.     good        feeling
                                                          0              0
 136 21  EffexorXR.21    2    Feel disconnected        disconnected   feel    
 136     EffexorXR.21                                     0            0
 142 22  EffexorXR.22    1    Weight gain               gain         
 142                     1                                1    

The final out put is like this:

  A  B          D        R        sentence              ADR1         ADR2     
 135 21  EffexorXR.21    1    lack of good feeling.     good        feeling
 135 21  EffexorXR.21    1                               1             0
 136 21  EffexorXR.21    2    Feel disconnected        disconnected   feel    
 136 21  EffexorXR.21    2                                 0            0
 142 22  EffexorXR.22    1    Weight gain               gain         
 142 22  EffexorXR.22    1                                1    

I can use copy, but I do not know how to iterate between rows. Any suggestion ?

1
  • What is the file format for the data set-- CSV, tab seperated txt, or something else? Commented Jun 25, 2017 at 13:10

1 Answer 1

5

Use fillna with method ffill what is same as ffill() if missing values are NaNs:

cols = ['A','B','D','R']
df[cols] = df[cols].ffill()

If missing values are empty strings:

cols = ['A','B','D','R']
df[cols] = df[cols].replace('',np.nan).ffill()
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.