1

I have the following pandas dataframe:

import pandas as pd
foo = pd.DataFrame({'week': [1,2,4,5,7], 'items':[1,2,3,4,5]}

I would like to fill foo with rows for the "missing" weeks from 1 to 7 and then for these rows the items column should have as value the previous non-na value

The output dataframe should look like this:

foo = pd.DataFrame({'week': [1,2,3,4,5,6,7], 'items':[1,2,2,3,4,4,5]}

How could I do that ?

quant
  • 4,062
  • 5
  • 29
  • 70
  • set_index, reindex with the range of min and max of weeks, then ffill., did you try this: https://stackoverflow.com/questions/19324453/add-missing-dates-to-pandas-dataframe – anky Apr 21 '21 at 14:04

1 Answers1

2

You can try with set_index() method,reindex() method,ffill() method and reset_index() method:

resultdf=foo.set_index('week').reindex(range(1,8)).ffill().reset_index()

Now If you print resultdf you will get:

     week      items
0     1        1.0
1     2        2.0
2     3        2.0
3     4        3.0
4     5        4.0
5     6        4.0
6     7        5.0

Now If you want items values in int then you can make use of astype() method:

resultdf['items']=resultdf['items'].astype(int)
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41