I have two DataFrames, one containing the main data of interest, and the other a lookup containing columns I wish to append to the former. For example:
df1
Name Date_1 Date_2
0 John 2019-11-13 2019-12-28
1 Amy 2019-11-13 2019-12-28
2 Sarah 2019-11-14 2019-12-29
3 Dennis 2019-11-14 2019-12-29
4 Austin 2019-11-15 2019-12-30
5 Jenn 2019-11-08 2019-12-23
df2
Var_1 Var_2
0 1x2 test_1
1 3x4 test_2
2 5x6 test_3
For each row in df2
, I would like to append Var_1
and Var_2
to df1
and populate all with the values from that row. This would be repeated for each unique row in df2
, resulting in a concatenated dataframe like so:
df_final
Name Date_1 Date_2 Var_1 Var_2
0 John 2019-11-13 2019-12-28 1x2 test_1
1 Amy 2019-11-13 2019-12-28 1x2 test_1
2 Sarah 2019-11-14 2019-12-29 1x2 test_1
3 Dennis 2019-11-14 2019-12-29 1x2 test_1
4 Austin 2019-11-15 2019-12-30 1x2 test_1
5 Jenn 2019-11-08 2019-12-23 1x2 test_1
6 John 2019-11-13 2019-12-28 3x4 test_2
7 Amy 2019-11-13 2019-12-28 3x4 test_2
8 Sarah 2019-11-14 2019-12-29 3x4 test_2
9 Dennis 2019-11-14 2019-12-29 3x4 test_2
10 Austin 2019-11-15 2019-12-30 3x4 test_2
11 Jenn 2019-11-08 2019-12-23 3x4 test_2
12 John 2019-11-13 2019-12-28 5x6 test_3
13 Amy 2019-11-13 2019-12-28 5x6 test_3
14 Sarah 2019-11-14 2019-12-29 5x6 test_3
15 Dennis 2019-11-14 2019-12-29 5x6 test_3
16 Austin 2019-11-15 2019-12-30 5x6 test_3
17 Jenn 2019-11-08 2019-12-23 5x6 test_3
My initial solution is to iterate over each row in df2
, appending columns Var_1
and Var_2
to df1
with the values of that row. I would then concatenate the resulting dataframes to create df_final
.
While this solution works, the dataframes will eventually become much larger so I feel like a more efficient solution does exist.