0

I have df1 and df2 and I want to merge these two df that extract Date_2 in df2 which are within the time period of Date_1 in df1.

df1 looks like:

Period   Date_1  Money
1        1/1      50
2        1/15     100
3        1/31     50
4        2/15     60
5        2/28     50

df2 looks like:

Date_2     Money
1/1        50
1/5        200
1/15       100
1/20       300
1/31       50
2/3        300
2/15       60
2/18       200

I would like the output be like:

Period     Date    Money
1          1/1      50
2          1/5      200 
2          1/15     100
3          1/20     300
3          1/31     50
4          2/3      300
4          2/15     60
5          2/18     200
5          2/28     50

1 Answers1

0
a=df1['Date_1'].tolist()
b=df2['Date_2'].tolist()

t=a+b
t=list(set(t))
df3=pd.DataFrame(t)
df3=df3.merge(df1,left_on=0,right_on='Date_1',how='left').merge(df2,left_on=0,right_on='Date_2',how='left')

df3['money']=df1['money_x']+df2['money_y']

I have not ran this code but should work more or less.

uxke
  • 446
  • 3
  • 9