df1=
Date | Brand |
---|---|
18-01-2022 | 'abc' |
15-02-2022 | 'xyz' |
df2=
Date | Qty |
---|---|
22-01-2022 | 11 |
18-02-2022 | 15 |
so that output looks like ---
Date | Brand | QTY |
---|---|---|
18-01-2022 | 'abc' | 11 |
15-02-2022 | 'xyz' | 15 |
CodePudding user response:
Convert the Date
to a week period and merge using that, and clean up the final data (i.e. drop extra columns)
import pandas as pd
df1 = pd.DataFrame({'Date': ['2022-01-18', '2022-02-15'], 'Brand': ['abc', 'xyz']})
df2 = pd.DataFrame({'Date': ['2022-01-22', '2022-02-18'], 'Qty': [11, 15]})
df1['week'] = pd.to_datetime(df1.Date).dt.to_period('W')
df2['week'] = pd.to_datetime(df2.Date).dt.to_period('W')
df1.merge(df2[['week', 'Qty']], on='week').drop(['week'], axis=1)
this produces output:
Date Brand Qty
0 2022-01-18 abc 11
1 2022-02-15 xyz 15