1

I have two df's like below,

df1

│ Row │ x1    │ x2    │ x3    │
│     │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┤
│ 1   │ 8     │ 1     │ 4     │
│ 2   │ 4     │ 3     │ 1     │
│ 3   │ 7     │ 8     │ 1     │

df2

│ Row │ x1    │ x2    │ x3    │ x4    │
│     │ Int64 │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┼───────┤
│ 1   │ 20    │ 14    │ 18    │ 100   │
│ 2   │ 13    │ 19    │ 17    │ 101   │
│ 3   │ 13    │ 10    │ 16    │ 102   │

When I perform vcat it throws, ArgumentError: column(s) x4 are missing from argument(s) 1 I understood this error because of mismatched column names. Is there any way I can still append these dataframes and put missing wherever it's not found.

Expected output:

6×3 DataFrame
│ Row │ x1    │ x2    │ x3    │ x4    |
│     │ Int64 │ Int64 │ Int64 │       |
├─────┼───────┼───────┼───────┤───────┤
│ 1   │ 8     │ 1     │ 4     │missing|
│ 2   │ 4     │ 3     │ 1     │missing|
│ 3   │ 7     │ 8     │ 1     │missing|
│ 4   │ 20    │ 14    │ 18    │100    |
│ 5   │ 13    │ 19    │ 17    │101    |
│ 6   │ 13    │ 10    │ 16    │102    |
Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111

1 Answers1

3

Just type:

vcat(df1, df2, cols=:union)

You can find this information by looking at the help for vcat:

julia> ?vcat
  
  ...

  Example
  ≡≡≡≡≡≡≡≡≡

  julia> df1 = DataFrame(A=1:3, B=1:3);

  ...

  julia> df3 = DataFrame(A=7:9, C=7:9);

  ...

  julia> vcat(df1, df3, cols=:union)
  6×3 DataFrame
  │ Row │ A     │ B       │ C       │
  │     │ Int64 │ Int64?  │ Int64?  │
  ├─────┼───────┼─────────┼─────────┤
  │ 1   │ 1     │ 1       │ missing │
  │ 2   │ 2     │ 2       │ missing │
  │ 3   │ 3     │ 3       │ missing │
  │ 4   │ 7     │ missing │ 7       │
  │ 5   │ 8     │ missing │ 8       │
  │ 6   │ 9     │ missing │ 9       │
Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62