I have two dataframes, a snippet looks like this:
year1 = {'DAY':['MON', 'MON', 'MON', 'TUE', 'TUE', 'TUE'],
'TEMP':[12, 13, 14, 15, 15, 18],
'DATE':['01/01/20', '02/01/20', '03/01/20', '06/01/20', '07/01/20', '08/01/20']}
df1 = pd.DataFrame(year1)
year2 = {'DAY':['MON', 'MON', 'MON', 'TUE', 'TUE', 'TUE'],
'TEMP':[15, 15, 15, 15, 14, 14],
'DATE':['01/01/20', '02/01/20', '03/01/20', '06/01/20', '07/01/20', '10/01/20']}
df2 = pd.DataFrame(year2)
The dataframes are NOT indexed on date (index is some other column). I want to merge the dataframes with rows where Date Values match in these two dataframes and add a new column based on date match:
df_FINAL['AVG_TEMP'] = (df1['TEMP'] + df2['TEMP']) / 2
So the resultant DataFrame should be like:
DAY TEMP DATE AVG_TEMP
0 MON 15 01/01/20 13.5
1 MON 15 02/01/20 14.0
2 MON 15 03/01/20 14.5
3 TUE 15 06/01/20 15.0
4 TUE 14 07/01/20 14.5
How to achieve this?