I have the two following dataframes:
df1
rate amount datetime Loc
1 14.8000 293976 2020-08-09 16:00:00 136
2 15.0000 1205745 2020-08-09 16:00:00 136
3 13.4000 249321 2020-08-09 16:00:00 136
4 13.0000 376737 2020-08-09 16:00:00 136
5 14.5000 224181 2020-08-09 20:00:00 137
6 14.8000 300443 2020-08-09 20:00:00 137
7 15.0000 1184055 2020-08-09 20:00:00 137
df2
Date Open High Low Close Loc
2020-08-09 00:00:00 12.7396 12.9097 12.4076 12.6492 136
2020-08-09 04:00:00 12.6515 13.7592 12.5184 13.5465 137
I need to add two columns to df1
, called df1['Close']
and df1['Open']
, and the value of these two values must be the value of the row in df2
that has the same Loc
.
So in this case the output should be:
rate amount datetime Loc Open Close
1 14.8000 293976 2020-08-09 16:00:00 136 12.7396 12.6492
2 15.0000 1205745 2020-08-09 16:00:00 136 12.7396 12.6492
3 13.4000 249321 2020-08-09 16:00:00 136 12.7396 12.6492
4 13.0000 376737 2020-08-09 16:00:00 136 12.7396 12.6492
5 14.5000 224181 2020-08-09 20:00:00 137 12.6515 13.5465
6 14.8000 300443 2020-08-09 20:00:00 137 12.6515 13.5465
7 15.0000 1184055 2020-08-09 20:00:00 137 12.6515 13.5465
Is there any way to do this, maybe with list comprehension? I've been looking into JOIN operations, but i'm not sure it's what i need here, since i don't need to fetch elements that the two dataframes have in common.