# Add a column in dataframe based on another column

**URL:** <https://discourse.julialang.org/t/add-a-column-in-dataframe-based-on-another-column/56275>\
**Category:** Data\
**Tags:** question\
**Created:** [March 1, 2021, 6:37pm UTC](https://discourse.julialang.org/t/add-a-column-in-dataframe-based-on-another-column/56275 "2021-03-01T18:37:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shazman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shazman/32/23093_2.png) [@Shazman](https://discourse.julialang.org/u/Shazman)\
**Post date:** [March 1, 2021, 6:37pm UTC](https://discourse.julialang.org/t/add-a-column-in-dataframe-based-on-another-column/56275/1 "2021-03-01T18:37:18Z")

</div>

Hi

I have two dataframe and the " IID " in A and " X " in B are same but order is not.  
I want to add column " phenotype " of B into A according to column IID.  
how can I do it?

> dataframe A

| | Herd.when.genotyped | X | IID | X.2 | X.3 | X.4 | X.5 | Herd.born.in |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| | String | Int64 | Int64 | Int64 | Int64 | Int64 | Int64 | String |
| 1 | 10041 | 0 | 412485541 | 412301635 | 410265426 | 2 | -9 | 10041 |
| 2 | 10041 | 0 | 411679484 | 412301206 | 410763797 | 2 | -9 | 10041 |
| 3 | 10041 | 0 | 412447988 | 412301927 | 412007106 | 2 | -9 | 10041 |
| 4 | 10041 | 0 | 412462041 | 412301974 | 411697516 | 2 | -9 | 10041 |
| 5 | 10041 | 0 | 410230957 | 412301866 | 408789139 | 2 | -9 | 10041 |
| 6 | 10041 | 0 | 412492631 | 412301972 | 408789139 | 2 | -9 | 10041 |
| 7 | 10041 | 0 | 412543616 | 412301961 | 410240054 | 2 | -9 | 10041 |
| 8 | 10041 | 0 | 409809986 | 412301206 | 410598606 | 2 | -9 | 10041 |
| 9 | 10041 | 0 | 412470980 | 412301923 | 409906717 | 2 | -9 | 10041 |
| 10 | 10041 | 0 | 412473967 | 412301946 | 410401245 | 2 | -9 | 10041 |
| 11 | 10041 | 0 | 410780712 | 412301355 | 409679783 | 2 | -9 | 10041 |
| 12 | 10041 | 0 | 412720229 | 0 | 0 | 2 | -9 | 10041 |
| 13 | 10041 | 0 | 412467596 | 412301927 | 410655683 | 2 | -9 | 10041 |
| 14 | 10041 | 0 | 411065373 | 412301845 | 410240054 | 2 | -9 | 10041 |

> dataframe B

| | X | Phenotype |
| --- | --- | --- |
| | Int64 | Float64 |
| 1 | 409859435 | -2.72553 |
| 2 | 409922125 | 1.45445 |
| 3 | 411075330 | 0.735245 |
| 4 | 412057132 | 0.78618 |
| 5 | 404693736 | 0.808009 |
| 6 | 404880845 | -1.22615 |
| 7 | 405021206 | 1.6335 |
| 8 | 405050763 | 0.77081 |
| 9 | 405061972 | 0.729143 |
| 10 | 405081272 | 3.60625 |
| 11 | 407123971 | -1.97556 |
| 12 | 408672181 | -4.40418 |
| 13 | 408796084 | -2.38459 |
| 14 | 408961344 | -0.991621 |
| 15 | 408980544 | -2.16643 |
| 16 | 409008076 | 3.44803 |
| 17 | 409057560 | -3.55253 |
| 18 | 409124877 | -1.11674 |
| 19 | 409304952 | -3.14657 |

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 1, 2021, 7:08pm UTC](https://discourse.julialang.org/t/add-a-column-in-dataframe-based-on-another-column/56275/2 "2021-03-01T19:08:16Z")

</div>

```julia
leftjoin(A, B, on = "IID")

```

?

---

<div class="post-metadata">

**Author:** ![Shazman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shazman/32/23093_2.png) [@Shazman](https://discourse.julialang.org/u/Shazman)\
**Post date:** [March 1, 2021, 7:25pm UTC](https://discourse.julialang.org/t/add-a-column-in-dataframe-based-on-another-column/56275/3 "2021-03-01T19:25:06Z")

</div>

@pdeffebach , I changed the column X to IID in B. then you command worked.  
but I am wondering how can I check that values in column “IID” of A are same as values in X column of B

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 1, 2021, 7:29pm UTC](https://discourse.julialang.org/t/add-a-column-in-dataframe-based-on-another-column/56275/4 "2021-03-01T19:29:09Z")

</div>

You can check if `Set(A.IID) == Set(B.IID)`

---

<div class="post-metadata">

**Author:** ![Shazman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shazman/32/23093_2.png) [@Shazman](https://discourse.julialang.org/u/Shazman)\
**Post date:** [March 1, 2021, 7:35pm UTC](https://discourse.julialang.org/t/add-a-column-in-dataframe-based-on-another-column/56275/5 "2021-03-01T19:35:21Z")

</div>

thank you for helping me today 😀
