I have 2 dataframes:
df1 with sales data:
key | date | sales
1 | 2020-10-16 | 100
1 | 2020-10-17 | 150
1 | 2020-10-19 | 180
2 | 2019-11-01 | 26
2 | 2019-11-02 | 27
2 | 2019-11-05 | 28
df2 with advertisement campaign data:
key | sale_start | sale_end | stock
1 | 2020-10-16 | 2020-10-18 | 1000
1 | 2020-10-17 | 2020-10-20 | 1500
1 | 2020-10-20 | 2020-10-31 | 1800
2 | 2019-11-01 | 2019-11-03 | 260
2 | 2019-11-03 | 2019-11-05 | 270
2 | 2019-11-05 | 2019-11-15 | 280
- I need to get the "stock" number of "df2" into "df1" so that every sales day of "df1" has the stock number as a column.
- then I need the % of saled products to stock for that day.
There are some overlapping campaigns, so the "stock" needs to be summed to for those overlapping days.
End result should be:
key | date | sales | stock | sales_stock_%
1 | 2020-10-16 | 100 | 1000 | 10
1 | 2020-10-17 | 150 | 2500 (1000 + 1500) | 6
1 | 2020-10-19 | 180 | 1500 | 12
2 | 2019-11-01 | 26 | 260 | 10
2 | 2019-11-02 | 27 | 260 | 10.38461538461538
2 | 2019-11-05 | 28 | 550 (270 + 280) | 5.090909090909091
Last column is easy but how can I add the stock to df1?