# Create new dataframe with minor changes

**URL:** <https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245>\
**Category:** New to Julia\
**Tags:** dataframes, copy\
**Created:** [April 29, 2022, 8:58am UTC](https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245 "2022-04-29T08:58:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [April 29, 2022, 8:58am UTC](https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245/1 "2022-04-29T08:58:51Z")

</div>

Hi ,  
I tried to create a new data frame with theminor changes from my original dataframe. But whenever i tried to change something in new dataframe, it gets changed in original as well. Can some one help me to solve this . here is the code i tried.

```julia
df1 = DataFrame(ID = (1:10), A = (11:20))
df2 = df1

df2[!,:A] = @. ifelse(df2.ID > 5, 30,df2.A)

```

---

<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:** [April 29, 2022, 9:25am UTC](https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245/2 "2022-04-29T09:25:42Z")

</div>

> [@sai\_matcha](#):
>
> `df2 = df1`

You should make a copy of value bound to `df1` variable:

```julia
df2 = copy(df1)

```

`df2 = df1` does not copy value. It just adds binding to the `df2` name of a value bound to `df1` name. This is discussed in [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#Mutable-Composite-Types) and additionally explained in chapter 2 of [Julia for Data Analysis](https://www.manning.com/books/julia-for-data-analysis) (the content should be available for you to view for free in the preview of the book).

A similar question is posted today for a second time so I also have written a post [Values’ mutability in Julia | Blog by Bogumił Kamiński](https://bkamins.github.io/julialang/2022/04/29/mutability.html) related to the issue of mutability (it covers a bit different aspect than `=` operator but I hope it will help you to understand the whole concept of mutable values better).

---

<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:** [April 29, 2022, 9:49am UTC](https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245/3 "2022-04-29T09:49:15Z")

</div>

Are you sure you’re not paying people to ask questions on here which are answered in your new book? 😃

---

<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:** [April 29, 2022, 9:58am UTC](https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245/4 "2022-04-29T09:58:02Z")

</div>

This was also astonishing to me. The issue of “binding vs copying” of value by assignment operator `=` is AFAICT not clearly explained in [The Julia Manual](https://docs.julialang.org/en/v1/). But maybe I missed the place where it is clearly laid out (in this case please let me know 😄). It is mentioned eg in. bullet 2 of [Noteworthy Differences from other Languages · The Julia Language](https://docs.julialang.org/en/v1/manual/noteworthy-differences/), but I think it is not a general exposition of the topic.

---

<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:** [April 29, 2022, 10:09am UTC](https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245/5 "2022-04-29T10:09:41Z")

</div>

I think the traditional link given to people asking questions about why `=` sometimes seems to mutate and sometimes not was

> **[Values vs. Bindings: The Map is Not the Territory](https://www.johnmyleswhite.com/notebook/2014/09/06/values-vs-bindings-the-map-is-not-the-territory/)**
>
> Many newcomers to Julia are confused by the seemingly dissimilar behaviors of the following two functions:
> julia\> a = \[1, 2, 3\] 3-element Array{Int64,1}: 1 2 3 julia\> function foo!(a) a\[1\] = 10 return end foo! (generic function with 1 method)...

---

<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:** [April 29, 2022, 12:51pm UTC](https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245/6 "2022-04-29T12:51:20Z")

</div>

Nice - I have never seen this, I need to add it to my list of references.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 29, 2022, 2:28pm UTC](https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245/7 "2022-04-29T14:28:16Z")

</div>

Also, note that if any of the elements of the columns are `mutable` themselves, and you may be mutate them, then maybe it is best to use `deepcopy` instead of `copy`.

---

<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:** [April 29, 2022, 2:37pm UTC](https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245/8 "2022-04-29T14:37:17Z")

</div>

`copy` for data frame makes `copy` of columns (but not `deepcopy`).

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 29, 2022, 4:35pm UTC](https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245/9 "2022-04-29T16:35:57Z")

</div>

Yes? This is what I said. If you need to `deepcopy` the columns then call `deepcopy` over the `DataFrame` instead of `copy`.

---

<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:** [April 29, 2022, 5:49pm UTC](https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245/10 "2022-04-29T17:49:00Z")

</div>

Ah - right. I misread it first.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 29, 2022, 9:06pm UTC](https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245/11 "2022-04-29T21:06:41Z")

</div>

> [@bkamins](#):
>
> The issue of “binding vs copying” of value by assignment operator `=` is AFAICT not clearly explained in [The Julia Manual](https://docs.julialang.org/en/v1/).

It seems to be covered in [Base \> Essentials section](https://docs.julialang.org/en/v1/base/base/#=)?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 30, 2022, 2:28am UTC](https://discourse.julialang.org/t/create-new-dataframe-with-minor-changes/80245/12 "2022-04-30T02:28:16Z")

</div>

That list really shows how poor `=` is given too much overwork by language designers, XD.
