# Is it possible to join two dataframes that returns a view of the original two?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-join-two-dataframes-that-returns-a-view-of-the-original-two/124296>\
**Category:** Data\
**Created:** [December 31, 2024, 1:38am UTC](https://discourse.julialang.org/t/is-it-possible-to-join-two-dataframes-that-returns-a-view-of-the-original-two/124296 "2024-12-31T01:38:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![okatsn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/okatsn/32/36138_2.png) [@okatsn](https://discourse.julialang.org/u/okatsn)\
**Post date:** [December 31, 2024, 1:38am UTC](https://discourse.julialang.org/t/is-it-possible-to-join-two-dataframes-that-returns-a-view-of-the-original-two/124296/1 "2024-12-31T01:38:42Z")

</div>

In julia programming, is it possible to join two dataframes, where the joined dataframe is essentially a “view” of the original two?  
For example,

```julia-repl
  julia> name = DataFrame(ID=[1, 2, 3], Name=["John Doe", "Jane Doe", "Joe Blogs"])
  3×2 DataFrame
   Row │ ID Name
       │ Int64 String
  ─────┼──────────────────
     1 │ 1 John Doe
     2 │ 2 Jane Doe
     3 │ 3 Joe Blogs
  julia> job = DataFrame(ID=[1, 2, 4], Job=["Lawyer", "Doctor", "Farmer"])
  3×2 DataFrame
   Row │ ID Job
       │ Int64 String
  ─────┼───────────────
     1 │ 1 Lawyer
     2 │ 2 Doctor
     3 │ 4 Farmer
  
  julia> df_join = leftjoin(name, job, on = :ID)
  3×3 DataFrame
   Row │ ID Name Job
       │ Int64 String String?
  ─────┼───────────────────────────
     1 │ 1 John Doe Lawyer
     2 │ 2 Jane Doe Doctor
     3 │ 3 Joe Blogs missing

```

such that

```julia
julia> df_join.Name[1] = "Foo Bar";
julia> df_join.ID[1] = 999;
julia> name.ID[1]
999
julia> job.ID[1]
999
julia> name.Name[1]
"Foo Bar"

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 31, 2024, 2:36am UTC](https://discourse.julialang.org/t/is-it-possible-to-join-two-dataframes-that-returns-a-view-of-the-original-two/124296/2 "2024-12-31T02:36:32Z")

</div>

`name`, `job`, and `df_join` all have separate `Vector{Int64}` for the ID column, they won’t share mutations. Even if `leftjoin` was able to make `name` and `df_join` share the ID vector, `job` still can’t share it because you provided a separate vector with different values in the first place.

You need to change types to pull off what you want. For example, you could make a `Ref.([1, 2, 3])` for `name`’s ID column and very carefully reuse those elements in all subsequent `Vector{Ref{Int64}}`. The vectors still won’t be shared so you don’t mutate those, but you mutate the `Ref` element.

I don’t recommend doing this everywhere because linking elements like this gets really messy. You could change a value that conflicts with another row, and that’s much easier to accidentally do when you’re mutating something in several tables at once. If you’re just mutating `Ref` instances, separate instances can end up referencing the same value, so you can’t be sure one `Ref(999)` is the same as the other `Ref(999)` when printed. I guess you could check a cache and mutate the vector’s element to an existing referenced value instead of mutating the reference every time, but that’s complicated to pull off and track too, and irreversibly decreasing the number of unique elements like that may not even be desirable. Making many `Ref` elements also hurts performance in general because of the lack of data locality in memory.
