# Change struct dataframe value correct way

**URL:** <https://discourse.julialang.org/t/change-struct-dataframe-value-correct-way/132121>\
**Category:** General Usage\
**Tags:** question, dataframes, struct\
**Created:** [September 5, 2025, 4:45pm UTC](https://discourse.julialang.org/t/change-struct-dataframe-value-correct-way/132121 "2025-09-05T16:45:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rdl987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdl987/32/218544_2.png) [@rdl987](https://discourse.julialang.org/u/rdl987)\
**Post date:** [September 5, 2025, 4:45pm UTC](https://discourse.julialang.org/t/change-struct-dataframe-value-correct-way/132121/1 "2025-09-05T16:45:52Z")

</div>

Hello I have a struct with inside other two structs:

```julia-auto
Base.@kwdef mutable struct MyDataset
  input::MyInputDataset = MyInputDataset()
  processed::MyInputDataset = MyInputDataset()
end

Base.@kwdef mutable struct MyInputDataset 
  first_st::FirstStruct = FirstStruct()
  second_st::SecondStruct = SecondStruct()
  df1::DataFrame = DataFrame()
  df2::DataFrame = DataFrame()
end

```

Now I instantiate the df1 dataframe inside the `input` struct and let the same dataframe from the `processed` struct to point to it

```julia-auto
julia> dataset.input.df1 = DataFrame("w"=>2)
1×1 DataFrame
 Row │ w     
     │ Int64
─────┼───────
   1 │ 2

julia> dataset.processed.df1 = dataset.input.df1 
1×1 DataFrame
 Row │ w     
     │ Int64
─────┼───────
   1 │ 2

```

After that if I want to change the dataframe of the `processed` it will change also the value of the `input`’s dataframe

```julia-auto
julia> dataset.processed.df1 = DataFrame("r"=>2)
1×1 DataFrame
 Row │ r     
     │ Int64
─────┼───────
   1 │ 2

julia> dataset.input.df1 
1×1 DataFrame
 Row │ r     
     │ Int64
─────┼───────
   1 │ 2

```

Could anyone explain me why and/or help me?

Thank you

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 5, 2025, 5:30pm UTC](https://discourse.julialang.org/t/change-struct-dataframe-value-correct-way/132121/2 "2025-09-05T17:30:53Z")

</div>

When you assign `dataset.processed.df1 = dataset.input.df1 `, you’re not creating a new dataframe with the same contents, instead you’re making `processed.df1` point to the same DataFrame as `input.df1`. This is generally how Julia works: assignment does not create a new copy.

If you want it to be a new DF, create a new dataframe with the [`copy` method](https://dataframes.juliadata.org/stable/lib/functions/#Base.copy), for eg. `dataset.processed.df1 = copy(dataset.input.df1; copycols=true)` .

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [September 5, 2025, 7:38pm UTC](https://discourse.julialang.org/t/change-struct-dataframe-value-correct-way/132121/3 "2025-09-05T19:38:19Z")

</div>

Welcome to the Julia community!

> [@rdl987](#):
>
> After that if I want to change the dataframe of the `processed` it will change also the value of the `input`’s dataframe
> 
> ```julia-auto
> julia> dataset.processed.df1 = DataFrame("r"=>2)
> 1×1 DataFrame
> Row │ r     
> │ Int64
> ─────┼───────
> 1 │ 2
> 
> julia> dataset.input.df1 
> 1×1 DataFrame
> Row │ r     
> │ Int64
> ─────┼───────
> 1 │ 2
> 
> ```

If I understand correctly, you’re showing desired behaviour here, i.e. you’d want `dataset.input.df1` to be as you printed (which is not how this currently works)? If so, then the situation is that you here reassign `dataset.processed.df1` to point to a new `DataFrame` (with `"r"`), while `dataset.input.df1` still refers to the old one (with `"w"`). Instead, you need to modify the original `DataFrame` inplace (using `!` methods). E.g.

```julia-repl
julia> dataset.processed.df1 = dataset.input.df1 = DataFrame("w"=>2);

julia> rename!(dataset.processed.df1, :w => :r) # Change the content of dataset.processed.df1, but don't reassign the variable
1×1 DataFrame
 Row │ r
     │ Int64
─────┼───────
   1 │ 2

julia> dataset.input.df1
1×1 DataFrame
 Row │ r
     │ Int64
─────┼───────
   1 │ 2

```

Alternatively, and for the same reasons, you could work with `Ref`s / `Base.RefValue`s.

---

<div class="post-metadata">

**Author:** ![rdl987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdl987/32/218544_2.png) [@rdl987](https://discourse.julialang.org/u/rdl987)\
**Post date:** [September 8, 2025, 7:39am UTC](https://discourse.julialang.org/t/change-struct-dataframe-value-correct-way/132121/4 "2025-09-08T07:39:27Z")

</div>

Thank you for the responses.

My idea is, at the beginning have the `processed.df1` to point to the same DataFrame as `input.df1` but at certain point into the time I want to let the `processed.df1` to point a new DataFrame leaveing the `input.df1` to point to the original Dataframe.

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [September 8, 2025, 9:44am UTC](https://discourse.julialang.org/t/change-struct-dataframe-value-correct-way/132121/5 "2025-09-08T09:44:51Z")

</div>

Well, this is already what your original code does?

```julia-repl
julia> dataset.input.df1 = DataFrame("w"=>2)
1×1 DataFrame
 Row │ w
     │ Int64
─────┼───────
   1 │ 2

julia> dataset.processed.df1 = dataset.input.df1
1×1 DataFrame
 Row │ w
     │ Int64
─────┼───────
   1 │ 2

julia> dataset.processed.df1 = DataFrame("r"=>2)
1×1 DataFrame
 Row │ r
     │ Int64
─────┼───────
   1 │ 2

julia> dataset.input.df1
1×1 DataFrame
 Row │ w
     │ Int64
─────┼───────
   1 │ 2

```

(@digital_carver 's reply is for when you want to decouple the `DataFrame`s while keeping the contents identical (for now), while mine was for keeping them coupled, while altering the common content.)

---

<div class="post-metadata">

**Author:** ![rdl987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdl987/32/218544_2.png) [@rdl987](https://discourse.julialang.org/u/rdl987)\
**Post date:** [September 8, 2025, 2:07pm UTC](https://discourse.julialang.org/t/change-struct-dataframe-value-correct-way/132121/6 "2025-09-08T14:07:32Z")

</div>

No because what I’m trying to achieve is to change only the `dataset.processed.df1` Dataframe without change also the `dataset.input.df1`.  
In other words at the begin I want that both `df1` dataframes point to the same memory location, so if I “query” `processed.df1` or `input.df1` I will get the same result. But at some point I will change the value **only** at `processed.df1` to point to a new memory location with a new DataFrame.

This request is because I need to get the initial data (`dataset.input`) “clean” to let the program to perform various run and every run will modify only the `dataset.processed` without perform a `copy()` or a `deepcopy()` to keep the RAM footprint as small as possible.

Thank you again

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [September 8, 2025, 3:02pm UTC](https://discourse.julialang.org/t/change-struct-dataframe-value-correct-way/132121/7 "2025-09-08T15:02:05Z")

</div>

> [@rdl987](#):
>
> No because what I’m trying to achieve is to change only the `dataset.processed.df1` Dataframe without change also the `dataset.input.df1`.

It really does sound like it’s already working as intended.

```julia-repl
julia> dataset.input.df1 = dataset.processed.df1 = DataFrame("w"=>2);

julia> dataset.input.df1 == dataset.processed.df1
true

julia> dataset.processed.df1 = DataFrame("r"=>2);

julia> dataset.input.df1 == dataset.processed.df1
false

```

> [@rdl987](#):
>
> In other words at the begin I want that both `df1` dataframes point to the same memory location, so if I “query” `processed.df1` or `input.df1` I will get the same result. But at some point I will change the value **only** at `processed.df1` to point to a new memory location with a new DataFrame.

```julia-repl
julia> dataset.input.df1 = dataset.processed.df1 = DataFrame("w"=>2);

julia> pointer_from_objref(dataset.input.df1)
Ptr{Nothing} @0x0000025ca5350c50

julia> pointer_from_objref(dataset.processed.df1) # same memory location
Ptr{Nothing} @0x0000025ca5350c50

julia> dataset.processed.df1 = DataFrame("r"=>2); # (or even DataFrame("w"=>2) )

julia> pointer_from_objref(dataset.input.df1) # has not changed
Ptr{Nothing} @0x0000025ca5350c50

julia> pointer_from_objref(dataset.processed.df1) # different
Ptr{Nothing} @0x0000025ca5351b50

```

> [@rdl987](#):
>
> This request is because I need to get the initial data (`dataset.input`) “clean” to let the program to perform various run and every run will modify only the `dataset.processed` without perform a `copy()` or a `deepcopy()` to keep the RAM footprint as small as possible.

I’m not sure I fully understand, but presumably you’d only need a single copy to create `dataset.processed.df1` from `dataset.input.df1`, after which you can just keep modifying `dataset.processed.df1` inplace? I don’t see how you could get a smaller memory footprint while keeping the `df1`s separate.
