How to add multiple columns to a dataframe at once

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:

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

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> 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

Another method which works is:

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

and cleaner version of @chris-b1 answer:

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

Third way:

DataFrames.hcat!(df,DataFrame(t',new_col_names))
2 Likes