# Difference result between insertcols! and transform! in spreading one column to many columns

**URL:** <https://discourse.julialang.org/t/difference-result-between-insertcols-and-transform-in-spreading-one-column-to-many-columns/51777>\
**Category:** Data\
**Tags:** question\
**Created:** [December 13, 2020, 9:38pm UTC](https://discourse.julialang.org/t/difference-result-between-insertcols-and-transform-in-spreading-one-column-to-many-columns/51777 "2020-12-13T21:38:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 13, 2020, 9:38pm UTC](https://discourse.julialang.org/t/difference-result-between-insertcols-and-transform-in-spreading-one-column-to-many-columns/51777/1 "2020-12-13T21:38:35Z")

</div>

I tried to understand how this script [here](https://www.juliabloggers.com/separating-a-column-into-multiple-columns-in-dataframes-jl/) works and I tried to reproduce it in other ways:

this with insertcols! it seems to work

```
     df=DataFrame(name=["James Smith 39","Michel Smith 41","Maria Garcia 19"], 
                                 freq=[12345,23431,11322])

     insertcols!( df,([:first,:last,:age] .=>collect.(zip(split.(df.name)...)))...)

```

instead trying to do something similar with transform! it does not work

```
     transform!(df, :name => (x-> collect.(zip(split.(x)...))).=>[:first,:last,:age])

```

could someone help me to find where the error is in the latter version?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 13, 2020, 10:35pm UTC](https://discourse.julialang.org/t/difference-result-between-insertcols-and-transform-in-spreading-one-column-to-many-columns/51777/2 "2020-12-13T22:35:34Z")

</div>

> [@rocco\_sprmnt21](#):
>
> ```julia
> transform!(df, :name => (x-> collect.(zip(split.(x)...))).=>[:first,:last,:age])
> 
> ```

You have an extra broadcasting at the end. You want

```julia
julia> transform!(df, :name => (x-> collect.(zip(split.(x)...)))=>[:first,:last,:age])

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 14, 2020, 7:44am UTC](https://discourse.julialang.org/t/difference-result-between-insertcols-and-transform-in-spreading-one-column-to-many-columns/51777/3 "2020-12-14T07:44:37Z")

</div>

Tanks!

I had been led by the analogy with the insertcols function syntax to use the form with braodcasting.  
I’m trying to understand what happens in each step.  
For example I have proved that the collect is not necessary.

```
              transform!(df, :name => (x-> (zip(split.(x)...)))=>[:first,:last,:age])

```

I’m also still trying to understand the various possibilities of the transform function.  
In one of the points in the manual it is explained that you can also use vectors of pairs (symbol, function).  
For this I tried the following form:

```
           transform!(df, [:name => (x->cs)=>cd for (cs,cd) in zip(split.(df.name),[:first,:last,:age])])

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 14, 2020, 11:04am UTC](https://discourse.julialang.org/t/difference-result-between-insertcols-and-transform-in-spreading-one-column-to-many-columns/51777/4 "2020-12-14T11:04:50Z")

</div>

I’m continuing to study the possibilities of the transform function !.  
this is very close to the one I was looking for in order to manage the splits of a column in a general way.

```
            transform!(df, :name => (x-> split.(x))=>AsTable)

            transform!(df, :name => (x-> split.(x))=>[string("col",i) for i in 1:length(split(df.name[1]))])

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 14, 2020, 7:48pm UTC](https://discourse.julialang.org/t/difference-result-between-insertcols-and-transform-in-spreading-one-column-to-many-columns/51777/5 "2020-12-14T19:48:05Z")

</div>

The most succint is probably

```julia
julia> transform(df, :name => ByRow(split) => AsTable)
3×5 DataFrame
 Row │ name freq x1 x2 x3        
     │ String Int64 SubStrin… SubStrin… SubStrin… 
─────┼─────────────────────────────────────────────────────────
   1 │ James Smith 39 12345 James Smith 39
   2 │ Michel Smith 41 23431 Michel Smith 41
   3 │ Maria Garcia 19 11322 Maria Garcia 19

```
