1
$\begingroup$

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.

$\endgroup$

1 Answer 1

2
$\begingroup$

You can use fillna function with ffill method:

df.fillna(method='ffill', inplace=True)
$\endgroup$
6
  • $\begingroup$ 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 $\endgroup$ Nov 21, 2019 at 13:56
  • $\begingroup$ What do they look like? Do they have any value preceding them or are they the first value in the column? $\endgroup$
    – serali
    Nov 21, 2019 at 14:20
  • $\begingroup$ it occurs only with the first row and there is no preceding to it. $\endgroup$ Nov 21, 2019 at 14:27
  • $\begingroup$ 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? $\endgroup$
    – serali
    Nov 21, 2019 at 14:29
  • $\begingroup$ 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. $\endgroup$ Nov 21, 2019 at 14:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.