0

I have dataset that looks like this

I want to iterate over each row and and want to check in each column if value is NaN if it is then i want to replace it with the previous value of the same row which is not null.

I believe the prefer way would be using lamba function. But still not figure out to code it Note: I have thousands of rows and 200 columns in each row

robinHood013
  • 209
  • 1
  • 2
  • 15

2 Answers2

1

The following should do the work:

df.fillna(method='ffill', axis=1, inplace=True)

Can you please clarify what you want to be done with NaNs in first column(s)?

IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
  • 1
    your code line basically replaces the NaN values with its previous value whichis not null. ANd it seems exaclty what i want – robinHood013 Sep 03 '20 at 18:08
1

i think you can use this -

your_df.apply(lambda x : x.fillna(method='ffill'), axis=1) 
vamsi_s
  • 176
  • 4