Create new dataframe with minor changes

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.

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

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

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

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 and additionally explained in chapter 2 of 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 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).

3 Likes

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

3 Likes

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. But maybe I missed the place where it is clearly laid out (in this case please let me know :smile:). It is mentioned eg in. bullet 2 of Noteworthy Differences from other Languages · The Julia Language, but I think it is not a general exposition of the topic.

2 Likes

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

3 Likes

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

1 Like

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.

2 Likes

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

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

1 Like

Ah - right. I misread it first.

1 Like

It seems to be covered in Base > Essentials section?

2 Likes

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