2

This is a new question after this, with more information

I want to merge two dataframes like the outer join, but I do not want the cartesian product, but only the concatenation, for example:

df1:
    A
0   2
1   2
2   2
3   2
4   2
5   3

df2:
    B
0   1
1   2
2   2
3   3
4   4

with : df3 = df1.merge(df2, left_on=['A'], right_on=['B'], how='outer') I get df3:

      A     B
0   2.0     2
1   2.0     2
2   2.0     2
3   2.0     2
4   2.0     2
5   2.0     2
6   2.0     2
7   2.0     2
8   2.0     2
9   2.0     2
10  3.0     3
11  NaN     1
12  NaN     

But I want:

   A    B
0   2.0     2
1   2.0     2
2   2.0     NaN
3   2.0     NaN
4   2.0     NaN
5   3.0     3
6   NaN     1
7   NaN     4

just concatenate the first 'm' of df1 with the m of df2 and dhe remaining values of df1 with a NaN value

linofex
  • 340
  • 1
  • 2
  • 17

2 Answers2

0

You might want to try/use the concat method. ex:

result = pd.concat([A, B], axis=1, sort=False)

You can read more here.

JohnDoe_Scientist
  • 590
  • 1
  • 6
  • 18
0

get the cumulative counts of A and B, and use the combination of the counts with A and B as merge conditions :

df1['checker'] = df1.groupby("A").cumcount()

df2['checker'] = df2.groupby("B").cumcount()

res = df1.merge(df2,left_on=['A','checker'],right_on=['B','checker'],how='outer').drop('checker',axis=1)


res 


     A  B
0   2.0 2.0
1   2.0 2.0
2   2.0 NaN
3   2.0 NaN
4   2.0 NaN
5   3.0 3.0
6   NaN 1.0
7   NaN 4.0
sammywemmy
  • 27,093
  • 4
  • 17
  • 31