0

I have two dataframes

key    value
A      1
B      2
C      3
D      4
E      5
key    value
A      2
C      6
D      1
F      8
G      9

When I merge horizontally, using the below code:

import pandas as pd

df1 = pd.DataFrame({
    'key': ["A", "B", "C", "D", "E"],
    'value': [1,2,3,4,5]
})

df2 = pd.DataFrame({
    'key': ["A", "C", "D", "F", "G"],
    'value': [2,6,1,8,9]
})
df_concat = pd.concat([df1, df2], axis=1)
print(df_concat)

the output is:

  key  value key  value
0   A      1   A      2
1   B      2   C      6
2   C      3   D      1
3   D      4   F      8
4   E      5   G      9

However, the output that I desire is:

key  value value
  A      1     2
  B      2     NaN
  C      3     6
  D      4     1
  E      5     NaN
  F      NaN   8
  G      NaN   9

I have referred the documentation but still am not able to do it.

Note: Values in value columns have no pattern. They are just random values.

Thanks in advance!

khelwood
  • 55,782
  • 14
  • 81
  • 108

1 Answers1

2

This sounds like a job for pd.merge:

pd.merge(df1, df2, how='outer', on='Key')

But since the Value column is common between the two DFs, you should probably rename them beforehand or something, as by default, the columns will be renamed as value_x and value_y.

Daren
  • 114
  • 5