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

**URL:** https://discourse.julialang.org/t/having-problems-to-modifying-a-dataframe-using-a-for-loop-and-eachcol/83143
**Category:** New to Julia
**Tags:** question
**Created:** [June 21, 2022, 8:29pm UTC](https://discourse.julialang.org/t/having-problems-to-modifying-a-dataframe-using-a-for-loop-and-eachcol/83143 "2022-06-21T20:29:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Juan\_Mac\_Donagh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan_mac_donagh/32/31798_2.png) [@Juan\_Mac\_Donagh](https://discourse.julialang.org/u/Juan_Mac_Donagh)
#### Post date: [June 21, 2022, 8:29pm UTC](https://discourse.julialang.org/t/having-problems-to-modifying-a-dataframe-using-a-for-loop-and-eachcol/83143/1 "2022-06-21T20:29:25Z")

</div>

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:

```julia
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:

```julia
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]
	end
	df1
end

```

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!

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [June 21, 2022, 8:43pm UTC](https://discourse.julialang.org/t/having-problems-to-modifying-a-dataframe-using-a-for-loop-and-eachcol/83143/2 "2022-06-21T20:43:34Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![Juan\_Mac\_Donagh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan_mac_donagh/32/31798_2.png) [@Juan\_Mac\_Donagh](https://discourse.julialang.org/u/Juan_Mac_Donagh)
#### Post date: [June 21, 2022, 8:51pm UTC](https://discourse.julialang.org/t/having-problems-to-modifying-a-dataframe-using-a-for-loop-and-eachcol/83143/3 "2022-06-21T20:51:49Z")

</div>

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