0

I know it might be super easy but I am new to dataframe operations. I have 17 dataframes in total and I need to merge them into a single one. For example:

DF1:

| id    | val_1    |
|-------|----------|
| A     |        0 |
| B     |        2 |
| C     |        1 |

DF2:

| id    | val_2    |
|-------|----------|
| A     |        5 |
| D     |        2 |

The desire result should be:

| id    | val_1    | val_2    |
|-------|----------|----------|
| A     |        0 |        5 |
| B     |        2 |      N/A |
| C     |        1 |      N/A |
| D     |      N/A |        2 |

What should be the correct method to use, merge or concate? Thank you!

Andy Ho
  • 45
  • 8

1 Answers1

0
import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.array([
    ['A', 0],
    ['B', 2],
    ['C', 1]]),
    columns=['id', 'val1'])
df2 = pd.DataFrame(np.array([
    ['A', 5],
    ['D', 2]]),
    columns=['id', 'val2'])

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

should give the desired output:

  id val1 val2
0  A    0    5
1  B    2  NaN
2  C    1  NaN
3  D  NaN    2
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156