0

Suppose we have:

>>> df1
   A  B
0  1  a
1  2  a
2  3  a
3  4  a

>>> df2
   A  B
0  1  b
1  2  b
2  3  b
3  5  b

I would like to merge them on "A" and then list them by interleaving rows like:

   A  B
0  1  a
0  1  b
1  2  a
1  2  b
2  3  a
2  3  b

I tried merge but it list them column by column. For example if I have 3 or more data frames, merge can merge them on some columns, but my problem would be then to interleave them

Ahmad
  • 8,811
  • 11
  • 76
  • 141

1 Answers1

1

If need match by A filter rows by Series.isin in boolean indexing, pass to concat with DataFrame.sort_index:

df = pd.concat([df1[df1.A.isin(df2.A)], 
                 df2[df2.A.isin(df1.A)]]).sort_index(kind='stable')
print (df)
   A  B
0  1  a
0  1  b
1  2  a
1  2  b
2  3  a
2  3  b

EDIT:

For general data is possible sorting by A and create default index for correct interleaving:

df = (pd.concat([df1[df1.A.isin(df2.A)].sort_values('A', kind='stable').reset_index(drop=True), 
                 df2[df2.A.isin(df1.A)].sort_values('A', kind='stable').reset_index(drop=True)])
        .sort_index(kind='stable'))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252