Situation: I have two dataframes df1 and df2, where df1 has a datetime index based on days, and df2 has two date columns 'wk start' and 'wk end' that are weekly ranges as well as one data column 'statistic' that stores data corresponding to the week range.
What I would like to do: Add to df1 a column for 'statistic' whereby I lookup each date (on a daily basis, i.e. each row) and try to find the corresponding 'statistic' depending on the week that this date falls into.
I believe the answer would require merging df2 into df1 but I'm lost as to how to proceed after that.
Appreciate any help you might provide! Thanks!
df1: (note: I skipped the rows between 2019-06-12 and 2019-06-16 to keep the example short.)
age | |
---|---|
date | |
2019-06-10 | 20 |
2019-06-11 | 21 |
2019-06-17 | 19 |
2019-06-18 | 18 |
df2:
wk start | wk end | statistic |
---|---|---|
2019-06-10 | 2019-06-14 | 102 |
2019-06-17 | 2019-06-21 | 100 |
2019-06-24 | 2019-06-28 | 547 |
2019-07-02 | 2019-07-25 | 268 |
Desired output:
age | statistic | |
---|---|---|
date | :--- | :-------- |
2019-06-10 | 20 | 102 |
2019-06-11 | 21 | 102 |
2019-06-17 | 19 | 100 |
2019-06-18 | 18 | 100 |
code for the dataframes d1 and d2
import pandas as pd
import datetime
data1 = {'date': ['2019-06-10', '2019-06-11', '2019-06-17', '2019-06-18'], 'age': [20, 21, 19, 18]}
data1['date']=pd.to_datetime(data1['date'])
df1 = pd.DataFrame(data1)
df1.set_index('date', inplace=True)
data2 = {'wk start': ['2019-06-10', '2019-06-17', '2019-06-24', '2019-07-02'], 'wk end':[ '2019-06-14', '2019-06-21', '2019-06-28', '2019-07-05'], 'height': [120,121, 119, 118]}
data2['wk start']=pd.to_datetime(data2['wk start'])
data2['wk end']=pd.to_datetime(data2['wk end'])
df2 = pd.DataFrame(data2)