Having problems to modifying a DataFrame using a for loop and eachcol

Hi all, I am having some issues with this:

I have a DataFrame that has a lot of columns that are strings, but I need to convert them to vectors of Ints, ie:

df = DataFrame( Mtx_1 = ("0 0 0 1 1 0"), Mtx_2 = ("0 0 0 0 1 0"))

The problem is that my code works fine when I’m broadcasting it to a single column (using df.Mtx_1 for example), but when I try to do it over all the columns, with a loop, it doesn’t modify the original DataFrame.

This is the code that I am using:

let df1 = copy(df)
	for col in eachcol(df1)
		col = split.(col ," ")
		#col = replace.(col, r"/"=>"0") #this is not necessary 
		col = [parse.(Float64, x) for x in col]

But here, df1 is not modified, so it returns the same columns where everything, ie, the columns are still Strings, and not Arrays.

I am using a let block only for convenience, so I can try different stuff, and I know that the code works for single columns, so I assume I am messing something in the loop.
Any idea why this happens? Thanks a lot!

Like in any Julia code, col = ... will just assign a new value to identifier col, but won’t mutate the original value that col pointed to. Use mapcols! instead:

mapcols!(df) do col
    [parse.(Float64, x) for x in split.(col ," ")]

Oh, now I know. It worked flawlessly, thanks a lot!

1 Like