0
                 df1
        date     hour      var1
0   2017-05-01  00:00:00   456585
1   2017-05-01  01:00:00   899875
2   2017-05-01  02:00:00   569566
3   2017-05-01  03:00:00   458756
4   2017-05-01  04:00:00   231458
5   2017-05-01  05:00:00   986545

                 df2
        date     hour      var2
0   2017-05-01  00:00:00   456585
1   2017-05-01  01:00:00   899875
2   2017-05-01  02:00:00   569566
3   2017-05-01  03:00:00   458756
4   2017-05-01  04:00:00   231458
5   2017-05-01  05:00:00   986545

Here are my two dataframes, I need to find a way that will allow me to transplant the column 'var2' from df2 and insert as a new column in df1 so that the dates and hours will match up.

Obviously how the dataframes look now I could just insert it and it would line up, but lets assume that the data is not aligned perfectly and would need some sort of conditional approach and transplant.

How would I go about this?

EDIT: Solved it using pandas merge as mentioned below!

index_vals = ['date', 'hour']
merged_inner = pd.merge(left=df1, right=df2, left_on=index_vals, right_on=index_vals)
Token Joe
  • 177
  • 1
  • 9

0 Answers0