# Programmatically adding multiple colums to a dataframe

**URL:** <https://discourse.julialang.org/t/programmatically-adding-multiple-colums-to-a-dataframe/111983>\
**Category:** General Usage\
**Tags:** dataframes, dataframesmeta\
**Created:** [March 22, 2024, 5:14pm UTC](https://discourse.julialang.org/t/programmatically-adding-multiple-colums-to-a-dataframe/111983 "2024-03-22T17:14:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pascal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal/32/207922_2.png) [@pascal](https://discourse.julialang.org/u/pascal)\
**Post date:** [March 22, 2024, 5:14pm UTC](https://discourse.julialang.org/t/programmatically-adding-multiple-colums-to-a-dataframe/111983/1 "2024-03-22T17:14:37Z")

</div>

Suppose I have the following code:

```julia
n = ["a_" * string(i) for i in 1:3]
v = [1,2,3]
df = DataFrame(a = [1, 2], b = [3, 4])
df_new = @chain df begin
    @transform $(n[1]) = v[1]
end

```

Can I change the `@transform` to add 3 new columns such that the result looks like

```julia
2×5 DataFrame
 Row │ a b a_1 a_2 a_3   
     │ Int64 Int64 Int64 Int64 Int64 
─────┼───────────────────────────────────
   1 │ 1 3 1 2 3
   2 │ 2 4 1 2 3

```

Thanks!

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [March 22, 2024, 5:28pm UTC](https://discourse.julialang.org/t/programmatically-adding-multiple-colums-to-a-dataframe/111983/2 "2024-03-22T17:28:06Z")

</div>

In vanilla DataFrames.jl the simplest way to do it is:

```julia
julia> insertcols(df, ["a_$i" => i for i in 1:3]...)
2×5 DataFrame
 Row │ a b a_1 a_2 a_3
     │ Int64 Int64 Int64 Int64 Int64
─────┼───────────────────────────────────
   1 │ 1 3 1 2 3
   2 │ 2 4 1 2 3

```

---

<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:** [March 22, 2024, 5:31pm UTC](https://discourse.julialang.org/t/programmatically-adding-multiple-colums-to-a-dataframe/111983/3 "2024-03-22T17:31:29Z")

</div>

Great questions. All the syntaxes within a chain block seem a bit clunky

```julia

julia> @chain df begin
           copy
           begin 
               for (ni, vi) in zip(n, v)
                   @rtransform! _ $ni = $vi
               end
               _
           end
       end
2×5 DataFrame
 Row │ a b a_1 a_2 a_3   
     │ Int64 Int64 Int64 Int64 Int64 
─────┼───────────────────────────────────
   1 │ 1 3 1 3 1
   2 │ 2 4 2 4 2

```

You can also leverage `AsTable` as follows

```julia
julia> @chain df begin
           @rtransform $AsTable = (; (Symbol(n[i]) => v[i] for i in eachindex(n))...)
       end
2×5 DataFrame
 Row │ a b a_1 a_2 a_3   
     │ Int64 Int64 Int64 Int64 Int64 
─────┼───────────────────────────────────
   1 │ 1 3 1 2 3
   2 │ 2 4 1 2 3

```

This is also very ugly. It looks like you can simplify things by using a `Dict`, though the order of columns might not be consistent, and performance will be hurt a little

```julia
julia> @chain df begin
           @rtransform $AsTable = Dict(n[i] => v[i] for i in eachindex(n))
       end
2×5 DataFrame
 Row │ a b a_1 a_2 a_3   
     │ Int64 Int64 Int64 Int64 Int64 
─────┼───────────────────────────────────
   1 │ 1 3 1 2 3
   2 │ 2 4 1 2 3

```

---

<div class="post-metadata">

**Author:** ![pascal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal/32/207922_2.png) [@pascal](https://discourse.julialang.org/u/pascal)\
**Post date:** [March 22, 2024, 5:36pm UTC](https://discourse.julialang.org/t/programmatically-adding-multiple-colums-to-a-dataframe/111983/4 "2024-03-22T17:36:55Z")

</div>

Thanks a lot, also to @bkamins. There’s a lot I can work with!

---

<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:** [March 22, 2024, 7:10pm UTC](https://discourse.julialang.org/t/programmatically-adding-multiple-colums-to-a-dataframe/111983/5 "2024-03-22T19:10:39Z")

</div>

> [@pascal](#):
>
> There’s a lot I can work with!

in decreasing order of weirdness

```julia
transform(df,Cols(:)=>((x...)->(;(n .=> v) ...))=>AsTable)
transform(df,:a=>((x)->(;(n .=> v) ...))=>AsTable)

insertcols(df, ["a_$i" => v[i] for i in 1:3]...)
insertcols(df, (n .=> v) ...)

```

```julia
DataFrame(pairs(eachcol(df))...,(n.=>v)...)

```
