# Adding information from one DataFrame to another

**URL:** <https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230>\
**Category:** New to Julia\
**Created:** [July 24, 2021, 11:20am UTC](https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230 "2021-07-24T11:20:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 24, 2021, 11:20am UTC](https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230/1 "2021-07-24T11:20:02Z")

</div>

I have a DataFrame that contains the columns “Date” and “Ticker”.

Suppose I have another DataFrame too that contains the columns “Date” and “ClosingPrice”.

How do I add the information from the second DataFrame to the first such that I end up with three columns, “Date”, “Ticker” and “ClosingPrice”?

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [July 24, 2021, 11:30am UTC](https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230/2 "2021-07-24T11:30:39Z")

</div>

sounds like u need `leftjoin`

---

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 24, 2021, 11:31am UTC](https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230/3 "2021-07-24T11:31:45Z")

</div>

Will that work without perfect correspondence between dates in the two DataFrames?

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [July 24, 2021, 11:32am UTC](https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230/4 "2021-07-24T11:32:52Z")

</div>

> [@Nash](#):
>
> perfect correspondence

generally no. you may want to google rolling joins and fuzzy joins and see if anything helps. haven’t used those in the julia ecosystem.

---

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 24, 2021, 11:41am UTC](https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230/5 "2021-07-24T11:41:07Z")

</div>

> [@xiaodai](#):
>
> rolling joins

I should probably illustrate specifically what I mean. For example:

DataFrameA:  
20-07-2021 IBM  
24-07-2021 IBM

DataFrameB:  
20-07-2021 IBM 100  
21-07-2021 IBM 101  
22-07-2021 IBM 102  
23-07-2021 IBM 103  
24-07-2021 IBM 104

Would left join work here?

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [July 24, 2021, 11:47am UTC](https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230/6 "2021-07-24T11:47:16Z")

</div>

try it.

also u haven’t shown ur desired result.

---

<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 24, 2021, 1:10pm UTC](https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230/7 "2021-07-24T13:10:00Z")

</div>

Hi @Nash

Is something like this what you’re looking for?

```julia
julia> A = DataFrame(date = [Date("2021-07-20"),Date("2021-07-24")],
                     symbol = ["IBM","IBM"])
2×2 DataFrame
 Row │ date symbol 
     │ Date String 
─────┼────────────────────
   1 │ 2021-07-20 IBM
   2 │ 2021-07-24 IBM

julia> B = DataFrame(date = [Date("2021-07-20"),Date("2021-07-21"),
                             Date("2021-07-22"),Date("2021-07-23"),
                             Date("2021-07-24")],
                     price = [100,101,102,103,104])
5×2 DataFrame
 Row │ date price 
     │ Date Int64 
─────┼───────────────────
   1 │ 2021-07-20 100
   2 │ 2021-07-21 101
   3 │ 2021-07-22 102
   4 │ 2021-07-23 103
   5 │ 2021-07-24 104

julia> leftjoin(B,A, on = :date)
5×3 DataFrame
 Row │ date price symbol  
     │ Date Int64 String? 
─────┼────────────────────────────
   1 │ 2021-07-20 100 IBM
   2 │ 2021-07-24 104 IBM
   3 │ 2021-07-21 101 missing 
   4 │ 2021-07-22 102 missing 
   5 │ 2021-07-23 103 missing 

```

For further details the documentation about joins with DataFrames is here: [Joins · DataFrames.jl](https://dataframes.juliadata.org/stable/man/joins/)

---

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 29, 2021, 11:46am UTC](https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230/8 "2021-07-29T11:46:55Z")

</div>

> [@viraltux](#):
>
> `leftjoin(B,A, on = :date)`

This solution is correct for the example I gave. But suppose A contained not only information about the ticker IBM but also the ticker MSFT. In that case, joining on date will add the price of IBM to MSFT too.

How can I place a condition to rule out that mistake?

---

<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 29, 2021, 11:50am UTC](https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230/9 "2021-07-29T11:50:58Z")

</div>

`leftjoin(B, A[:, Not(:MSFT)], on = :date)` assuming that `MSFT` is a column in `A`.

---

<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 29, 2021, 12:01pm UTC](https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230/10 "2021-07-29T12:01:56Z")

</div>

Generally you can use `leftjoin(B, A, on = [:ticker, :date])`, but it wouldn’t make much sense in this case, since you already have `symbol` column in `B`, so it’s hard to understand how result should look like.

---

<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 29, 2021, 12:15pm UTC](https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230/11 "2021-07-29T12:15:03Z")

</div>

Ah sorry I hadn’t scrolled up to see what the data actually looks like - I think your suggestion is correct (`B` has only `IBM` in the `ticker` column, so if `A` has `MSFT` and `IBM` in `ticker` then joining on both will only take the `IBM` entries from `A`)

---

<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 29, 2021, 2:22pm UTC](https://discourse.julialang.org/t/adding-information-from-one-dataframe-to-another/65230/12 "2021-07-29T14:22:26Z")

</div>

> [@Nash](#):
>
> How can I place a condition to rule out that mistake?

Hi @Nash

To make sure we are giving you the right answer and make it easier for us to help you perhaps is best if you build a **DataFrame**  **A** and **B** as in the example above and then write manually the results you would like to have after joining.

You can start loading the packages DataFrames and Dates like this:

`using DataFrames, Dates`

and then

```julia
A = DataFrame(date = [Date("2021-07-20"), 
                                     Date("2021-07-24"),  
                                     Date("2021-07-24")],
                     symbol = ["IBM","IBM","MSFT"])
B = ... etc.

```

Thanks! 🙂
