Combining a dataframe with a Vector{Float64}, and naming the new column something other than "x1"

I have the following code:

`DataFrame([df  new_column])`

where df is an existing dataframe and new_column is a Vector{Float64}.

The result creates a new df where the content of new_column has the name “x1”. How can I set the name of new_column to something specific as I create the new dataframe?

df.apples=randn(10)
2 Likes

I believe the typical method is df.my_specifc_name = new_column or if you want more control over where in the dataframe the column ends up, try insertcols!
Functions · DataFrames.jl!

Agree with the above but more verbose (and maybe explicit?) is:

julia> using DataFrames

julia> df = DataFrame(rand(3, 3), :auto)
3×3 DataFrame
 Row │ x1        x2        x3
     │ Float64   Float64   Float64
─────┼──────────────────────────────
   1 │ 0.947632  0.327634  0.252124
   2 │ 0.631133  0.575395  0.657919
   3 │ 0.13491   0.782569  0.222796

julia> insertcols!(df, "new_col" => rand(3))
3×4 DataFrame
 Row │ x1        x2        x3        new_col
     │ Float64   Float64   Float64   Float64
─────┼────────────────────────────────────────
   1 │ 0.947632  0.327634  0.252124  0.74787
   2 │ 0.631133  0.575395  0.657919  0.7908
   3 │ 0.13491   0.782569  0.222796  0.691634
1 Like