Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

julia dataframe changing one cell changes entire row

Consider the following code, where I want to change only 1 cell, but the entire row gets changed:

df=DataFrames.DataFrame(A=[1,2],B=[3,4])

df[2,:A]=7 # this is OK, changes only 1 cell

df[:,1:end]=0.0 # this line somehow makes the change in the next line behave unexpectedly

df[2,:A]=7 # entire 2nd row is 7

It's as if the df[:,1:end]=0.0 sets all the cells of that row to the same reference; but I am setting it to 0.0, so I expect this to be a value copy, not reference copy

Versions: julia version 0.4.6-pre DataFrames v"0.7.8"

like image 660
imran Avatar asked Sep 04 '16 19:09

imran


People also ask

How do I add a row to a DataFrame Julia?

Adding extra rows and columns to the data frame New rows can be added to the end of a data frame by using push!() function.

How do I change the column name in Julia DataFrame?

Additionally, in your example, you should use select! in order to modify the column names in place, or alternatively do 'df = select(df, "col1" => "Id", "col2" => "Name")` as select always return a new DataFrame .


1 Answers

There's some aliasing going on here. I think this is a bug in DataFrames, though it's possible that it's intended behaviour, albeit strange. What's happening is that the same underlying data is being used by both columns. See #1052.

As a workaround, you can set the columns one by one:

for c in 1:size(df, 2)
    df[:,c] = 0.0
end
like image 85
Fengyang Wang Avatar answered Sep 30 '22 11:09

Fengyang Wang