# Dataframe Joins in a loop

**URL:** <https://discourse.julialang.org/t/dataframe-joins-in-a-loop/83476>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [June 28, 2022, 7:05pm UTC](https://discourse.julialang.org/t/dataframe-joins-in-a-loop/83476 "2022-06-28T19:05:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guido](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@Guido](https://discourse.julialang.org/u/Guido)\
**Post date:** [June 28, 2022, 7:05pm UTC](https://discourse.julialang.org/t/dataframe-joins-in-a-loop/83476/1 "2022-06-28T19:05:25Z")

</div>

Hi all, I’m learning Julia and I’m stuck trying to merge several dataframes into only one using a loop.

For instances, suppose that I have 4 dataframes called A, B, C and D. They have the same column names except for the last one, named x4. And I want to merge all of them into one. So far, I’m doing this:

df1=innerjoin(A, B, on = [:x1, :x2, :x3])  
df2=innerjoin(df1, C, on = [:x1, :x2, :x3])  
df3=innerjoin(df2, D, on = [:x1, :x2, :x3])

How can I do this in a nice loop?

Thanks a lot!

---

<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:** [June 28, 2022, 7:30pm UTC](https://discourse.julialang.org/t/dataframe-joins-in-a-loop/83476/2 "2022-06-28T19:30:50Z")

</div>

If you don’t need a loop you can do

```julia
reduce((x, y) -> innerjoin(x, y, on =[:x1, :x2, :x3]), [A, B, C])

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [June 28, 2022, 8:34pm UTC](https://discourse.julialang.org/t/dataframe-joins-in-a-loop/83476/3 "2022-06-28T20:34:58Z")

</div>

Why do you want a loop? Normally you can just write:

```julia
innerjoin(A, B, C, D, on = [:x1, :x2, :x3], makeunique=true)

```

`makeunique=true` is needed as I understand that you have the same column name `:x4` in all data frames.

---

<div class="post-metadata">

**Author:** ![Guido](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@Guido](https://discourse.julialang.org/u/Guido)\
**Post date:** [June 29, 2022, 7:40pm UTC](https://discourse.julialang.org/t/dataframe-joins-in-a-loop/83476/4 "2022-06-29T19:40:15Z")

</div>

Thanks! It works well.

---

<div class="post-metadata">

**Author:** ![Guido](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@Guido](https://discourse.julialang.org/u/Guido)\
**Post date:** [June 29, 2022, 7:40pm UTC](https://discourse.julialang.org/t/dataframe-joins-in-a-loop/83476/5 "2022-06-29T19:40:49Z")

</div>

Thanks! It worked.
