0
tbl_1:

+---+---+---+
| a | b | c |
+---+---+---+
| 1 | 2 | x |
| 3 | 4 | y |
| 5 | 6 | z |
+---+---+---+

tbl_2:

+---+---+---+
| a | b | c |
+---+---+---+
| 1 | 1 | a |
| 3 | 4 | b |
| 5 | 6 | c |
+---+---+---+

How do I append the duplicate rows in tbl_2 to tbl_1 as follows:

+---+---+---+
| a | b | c |
+---+---+---+
| 1 | 2 | x |
| 3 | 4 | y |
| 5 | 6 | z |
| 3 | 4 | b |
| 5 | 6 | c |
+---+---+---+

I've tried using duplicated() but I can't figure out how to go from comparing within one DataFrame to comparing between DataFrames...

Jossy
  • 589
  • 2
  • 12
  • 36

1 Answers1

1

you can use pd.concat of tbl_1 and after selecting the rows from tbl_2 that exist en tbl_1 in the two columns (I assume a and b) with a inner merge:

print (pd.concat([tbl_1, 
                  tbl_1[['a','b']].merge(tbl_2, how='inner')]))
   a  b  c
0  1  2  x
1  3  4  y
2  5  6  z
0  3  4  b
1  5  6  c
Ben.T
  • 29,160
  • 6
  • 32
  • 54