I have two pandas dataframes, df1 and df2. Both having atleast two columns with the same name (c1 and c2) and then there are other columns, such as:
df1
-----------------
c1 c2 c4 c5
-----------------
10 1 hh 2231
11 1 fgf 2142
12 1 fg 1232
df2
-----------------
c1 c2 c6 c7
-----------------
10 2 110 231
10 3 111 332
11 2 112 123
11 3 113 432
12 2 114 432
12 3 115 432
13 2 116 432
13 3 117 432
14 2 118 432
14 3 119 432
I want to merge the two dataframes such that:
- There is a union of c1 and c2 from both the df
- c4 and c5 are repeated for all the instances of their corresponding c1 and c2 values from df1
- The remaining columns, i.e., c6 and c7 are matched against c1 and c2 in df2 and matched to the c1 value in df1 and then added to the resulting df.
In a previous question, I got the answer to perform outer join and I performed outer join based on c1 and c2 and that returns the following for example:
c1 c2 c4 c5 c6 c7
--------------------------------
10 1 hh 2231
10 2 110 231
10 3 111 332
However, I would like to repeat the values of c4 and c5 as the resulting df as follows:
resulting dataframe:
c1 c2 c4 c5 c6 c7
-----------------------------------
10 1 hh 2231
10 2 hh 2231 110 231
10 3 hh 2231 111 332
11 1 fgf 2142
11 2 fgf 2142 112 123
11 3 fgf 2142 113 432
12 1 fg 1232
12 2 fg 1232 114 432
12 3 fg 1232 115 432
13 1 dd 4532
13 2 dd 4532 116 432
13 3 dd 4532 117 432
14 2 118 432
14 3 119 432
Can anyone help me in this matter? Thanks in advance!