1

enter image description here

In the above sample data, I have empty fields and now the task is to fill the fields with previous values. my columns are dates and the values are a number of items present for that particular article for the specific date. which would be a faster way to interpolate the missing fields. Any suggestions to build the function is appreciated.

1 Answers1

2

You can use fillna function with ffill method:

df.fillna(method='ffill', inplace=True)
serali
  • 1,212
  • 1
  • 7
  • 19
  • I converted my empty fields with df.replace(r'^\s*$', np.nan, regex=True,inplace=True) and applied df.fillna. But still have some values which has nan – LIONEL JOSEPH Nov 21 '19 at 13:56
  • What do they look like? Do they have any value preceding them or are they the first value in the column? – serali Nov 21 '19 at 14:20
  • it occurs only with the first row and there is no preceding to it. – LIONEL JOSEPH Nov 21 '19 at 14:27
  • That is the reason, ffill uses the previous value to fill the next. First row has nothing before it so it cannot be filled by this method. I thought that was the what you wanted? – serali Nov 21 '19 at 14:29
  • im so stupid, my apologies, I wanted to fill it with the previous values. not the above values. for example, the article LL-23896 has nan need to fill them with 0 and the next values to be interpolated. I hope i was clear. – LIONEL JOSEPH Nov 21 '19 at 14:36
  • in that case, you need to use "df.fillna(method='bfill', inplace=True). Replace "ffill" method with "bfill". – serali Nov 21 '19 at 14:46