# How to merge 2 dataframes (DataFrames.jl)

**URL:** <https://discourse.julialang.org/t/how-to-merge-2-dataframes-dataframes-jl/64321>\
**Category:** General Usage\
**Tags:** question, dataframes\
**Created:** [July 9, 2021, 5:43am UTC](https://discourse.julialang.org/t/how-to-merge-2-dataframes-dataframes-jl/64321 "2021-07-09T05:43:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Based](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/based/32/24463_2.png) [@Based](https://discourse.julialang.org/u/Based)\
**Post date:** [July 9, 2021, 5:43am UTC](https://discourse.julialang.org/t/how-to-merge-2-dataframes-dataframes-jl/64321/1 "2021-07-09T05:43:02Z")

</div>

I have 2 dataframes. Each contains independent datapoints in different rows with different `ID`s. They share most of the same columns, except for one. I want to combine them into one data frame. I feel like there should be a function that does it, but I can’t find it. So far as I can tell `outerjoin` should do this, but it doesn’t work:

```julia
using DataFrames                                                                                                     
test1 = DataFrame(ID=1:3, Name=["A", "B", "C"], Argh=[5.0, 62.1, 89.0])
test2 = DataFrame(ID=4:6, Name=["D", "E", "F"])
testmerge = outerjoin(test1, test2, on=:ID)

```

result

```julia
ERROR: LoadError: ArgumentError: Duplicate variable names: :Name. Pass makeunique=true to make them unique using a suffix automatically.

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [July 9, 2021, 5:56am UTC](https://discourse.julialang.org/t/how-to-merge-2-dataframes-dataframes-jl/64321/2 "2021-07-09T05:56:37Z")

</div>

Have you tried to do, what they say in error message? Pass `makeunique=true`?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 9, 2021, 6:02am UTC](https://discourse.julialang.org/t/how-to-merge-2-dataframes-dataframes-jl/64321/3 "2021-07-09T06:02:37Z")

</div>

I agree with the above, although it seems to me like you are just trying to vertically concatenate the DataFrames rather than `join` them?

```julia
julia> vcat(test1, test2, cols = :union)
6×3 DataFrame
 Row │ ID Name Argh      
     │ Int64 String Float64?  
─────┼──────────────────────────
   1 │ 1 A 5.0
   2 │ 2 B 62.1
   3 │ 3 C 89.0
   4 │ 4 D missing   
   5 │ 5 E missing   
   6 │ 6 F missing   

```

(note the `cols = :union` argument which fills in the missing `Argh` column in `test2` with `missing`)

---

<div class="post-metadata">

**Author:** ![Based](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/based/32/24463_2.png) [@Based](https://discourse.julialang.org/u/Based)\
**Post date:** [July 9, 2021, 6:16am UTC](https://discourse.julialang.org/t/how-to-merge-2-dataframes-dataframes-jl/64321/4 "2021-07-09T06:16:14Z")

</div>

`makeunique=true` creates a bizarre dataframe with duplicate columns:

```julia
julia> testmerge
6×4 DataFrame
 Row │ ID Name Argh Name_1  
     │ Int64 String? Float64? String? 
─────┼────────────────────────────────────
   1 │ 1 A 5.0 missing 
   2 │ 2 B 62.1 missing 
   3 │ 3 C 89.0 missing 
   4 │ 4 missing missing D
   5 │ 5 missing missing E
   6 │ 6 missing missing F

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 9, 2021, 6:17am UTC](https://discourse.julialang.org/t/how-to-merge-2-dataframes-dataframes-jl/64321/5 "2021-07-09T06:17:28Z")

</div>

That’s not bizarre at all, that’s just what you asked for - you are `join`ing two DataFrames, both with a `Name` column, but you’re **not** joining **on** `Name`, so the new DataFrame will have two `Name` columns. As I said above, I think you’re just not looking for a join here…

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [July 9, 2021, 10:03pm UTC](https://discourse.julialang.org/t/how-to-merge-2-dataframes-dataframes-jl/64321/6 "2021-07-09T22:03:06Z")

</div>

If you feel confortable with SQL you can also do:

```julia
using SQLdf

test1 = DataFrame(ID=1:3, Name=["A", "B", "C"], Argh=[5.0, 62.1, 89.0])
test2 = DataFrame(ID=4:6, Name=["D", "E", "F"])

testmerge = sqldf("""
    select * from test1
    union
    select *, 0.0 from test2
    """)

6×3 DataFrame
 Row │ ID Name Argh    
     │ Int64 String Float64 
─────┼────────────────────────
   1 │ 1 A 5.0
   2 │ 2 B 62.1
   3 │ 3 C 89.0
   4 │ 4 D 0.0
   5 │ 5 E 0.0
   6 │ 6 F 0.0

```
