# How to add multiple columns to a dataframe at once

**URL:** <https://discourse.julialang.org/t/how-to-add-multiple-columns-to-a-dataframe-at-once/91007>\
**Category:** General Usage\
**Created:** [November 29, 2022, 4:57pm UTC](https://discourse.julialang.org/t/how-to-add-multiple-columns-to-a-dataframe-at-once/91007 "2022-11-29T16:57:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adz/32/33773_2.png) [@adz](https://discourse.julialang.org/u/adz)\
**Post date:** [November 29, 2022, 4:57pm UTC](https://discourse.julialang.org/t/how-to-add-multiple-columns-to-a-dataframe-at-once/91007/1 "2022-11-29T16:57:05Z")

</div>

I have a matrix and a vector of names. I want to add the elements of t to my dataframe, df. However, when I try to set multiple columns at once, my code fails. See the example below:

```julia
df = DataFrame(A=rand(4), B=rand(4))
new_col_names = ["C","D"]
t = rand(2,4)

### Does not work
df[:, new_col_names] = t'

### Works
df[:,"C"] = t[1,:]

### Works
df[:, ["A", "B"]] = t'

```

The bit that does not work throws the following error: ArgumentError: column name “C” not found in the data frame; existing most similar names are: “A” and “B”

I realize I can do this using a for loop, but is there a reason why multiple new columns cannot be assigned at once? And is there an alternative, aside from a for loop?

EDIT: added a transpose operator that I had forgotten to type

---

<div class="post-metadata">

**Author:** ![chris-b1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris-b1/32/14165_2.png) [@chris-b1](https://discourse.julialang.org/u/chris-b1)\
**Post date:** [November 29, 2022, 5:07pm UTC](https://discourse.julialang.org/t/how-to-add-multiple-columns-to-a-dataframe-at-once/91007/2 "2022-11-29T17:07:37Z")

</div>

Might be a cleaner way to write this, but the `insertcols!` function takes a variable number of pairs (`col_name => col_data`) to add multiple columns at once.

```julia
julia> insertcols!(df, map(p -> Pair(p...), zip(new_col_names, eachrow(t)))...)
4×4 DataFrame
 Row │ A B C D
     │ Float64 Float64 Float64 Float64
─────┼─────────────────────────────────────────
   1 │ 0.888737 0.0761829 0.104363 0.686945
   2 │ 0.745944 0.41055 0.474104 0.785596
   3 │ 0.381561 0.582091 0.93381 0.823993
   4 │ 0.520242 0.769232 0.755399 0.362647

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [November 29, 2022, 5:37pm UTC](https://discourse.julialang.org/t/how-to-add-multiple-columns-to-a-dataframe-at-once/91007/4 "2022-11-29T17:37:44Z")

</div>

Another method which works is:

```julia
setproperty!.(Ref(df), new_col_names, eachrow(t));

```

and cleaner version of @chris-b1 answer:

```julia
insertcols!(df, (new_col_names .=> eachrow(t))...)

```

Third way:

```julia
DataFrames.hcat!(df,DataFrame(t',new_col_names))

```
