I am trying to fill missing values for group (area code,shop_name,item_name,date,sales_amount). For each group, I need to fill the sales_amount with 52 weeks of data. I need to create two different columns with ffill(df.ffill()) and bfill (df.bfill()) and then I need to sum(the newly created columns with ffill & bfill/2 to obtain my result.
area_code shop_name item_name week_date sales_amount
101 Global Market Mango Fruits 6/3/2018 5.13
101 Global Market Mango Fruits 6/10/2018 nan
101 Global Market Mango Fruits 6/17/2018 7.13
101 Global Market Chips 6/3/2018 5
101 Global Market Chips 6/10/2018 nan
102 Global Market Mango Fruits 6/3/2018 10.34
102 Global Market Mango Fruits 6/10/2018 nan
102 Global Market Chips 6/10/2018 nan
102 Global Market Chips 6/17/2018 nan
102 Global Market Chips 6/24/2018 nan
102 Global Market Potato 6/24/2018 nan
After
area_code shop_name item_name week_date sales_amount
101 Global Market Mango Fruits 6/3/2018 5.13
101 Global Market Mango Fruits 6/10/2018 6.13
101 Global Market Mango Fruits 6/17/2018 7.13
101 Global Market Chips 6/3/2018 5
101 Global Market Chips 6/10/2018 5
102 Global Market Mango Fruits 6/3/2018 10.34
102 Global Market Mango Fruits 6/10/2018 10.34
102 Global Market Chips 6/10/2018 Value available before this week for this group
102 Global Market Chips 6/17/2018 Value available before this week for this group
102 Global Market Chips 6/24/2018 Value available before this week for this group
102 Global Market Potato 6/24/2018 Value available before this week for this group
For example -
Week 1 10
Week 2 nan
week 3 nan
"Value available before this week for this group" means that week 3, week 2 will have same value as week 1. Otherwise, If week 1 and 3 has some data , then fill week 2 according to ffill or bfill. if it doesn like in this, then simply fill the value by ffill or bfill for each group.
- How to iterate over dataframe?
- How to iterate over each groups and fill the value?
I tried using but didnt get any luck
My week data that needs to be fill starts with 6/3/2018 and ends at week 6/3/2019