Creating dataframe with column name that is a variable

All of the other columns are correct, i.e. I want “StartPoint” to be the name of the column and it has the data in the variable start_space. Below, param is a string variable. How do I tell it that I want the column name to be the value of the string variable? For example, when param = “diffusion”, I want the column name to be “diffusion” based on the param variable.

speed_df = DataFrame(StartPoint = start_space, EndPoint = end_space,
TimeInterval = time_interval, DistanceInterval = distance_interval,
Speed = speed, param = key)

1 Like

Sorry, this is wrong. Given that it didn’t work above, I should have tried

speed_df = rename!(speed_df, :param => param).

Edit: Below is wrong.

I even tried

speed_df = rename!(speed_df, :param => key),

but it returned with

LoadError: MethodError: no method matching rename!(::DataFrame, ::Vector{Pair{Symbol, Float64}})

Closest candidates are:

rename!(::AbstractDataFrame, !Matched::AbstractVector{Symbol}; makeunique) at /home/wesley/.julia/packages/DataFrames/zXEKU/src/abstractdataframe/abstractdataframe.jl:170

rename!(::AbstractDataFrame, !Matched::AbstractVector{var"#s3"} where var"#s3"<:AbstractString; makeunique) at /home/wesley/.julia/packages/DataFrames/zXEKU/src/abstractdataframe/abstractdataframe.jl:176

rename!(::AbstractDataFrame, !Matched::AbstractVector{Pair{Symbol, Symbol}}) at /home/wesley/.julia/packages/DataFrames/zXEKU/src/abstractdataframe/abstractdataframe.jl:182

in expression starting at /home/wesley/repos/pde/juliatest.jl:149

rename!(df::DataFrame, args::Pair{Symbol, Float64}) at abstractdataframe.jl:208

calc_speed(df::DataFrame, h::Float64, k::Float64, param::String, key::Float64) at juliatest.jl:86

process(a::Int64, b::Int64, T::Int64, t_steps::Int64, param::String, r_start::Float64, r_end::Int64, r_length::Int64) at juliatest.jl:142

top-level scope at juliatest.jl:149

eval at boot.jl:360 [inlined]

include_string(mapexpr::typeof(identity), mod::Module, code::String, filename::String) at loading.jl:1094

I am not sure if I understand your question properly, but I try:

julia> using DataFrames

julia> df=DataFrame( A = [1,2,3] , B = [4,5,6] )
3×2 DataFrame
 Row │ A      B
     │ Int64  Int64
─────┼──────────────
   1 │     1      4
   2 │     2      5
   3 │     3      6

julia> colname="X"
"X"

julia> rename!(df,:B => colname )
3×2 DataFrame
 Row │ A      X
     │ Int64  Int64
─────┼──────────────
   1 │     1      4
   2 │     2      5
   3 │     3      6

Is this what you like to achieve?

1 Like

Yes. You’ve understood correctly.

How about:

julia> param = :hello
:hello

julia> df = DataFrame(Dict(:a => [1, 2, 3], param=>[4, 5, 6]))
3×2 DataFrame
 Row │ a      hello 
     │ Int64  Int64 
─────┼──────────────
   1 │     1      4
   2 │     2      5
   3 │     3      6
2 Likes

Because I corrected my rename, it does yield the desired result.

rdeits,

Is there a way to make it work in my DataFrame definition? It seems like I need to tell Julia to not take the param literally but use the param variable value.

You can do this

param = "newvar"
julia> using DataFrames

julia> param = "newvar";

julia> DataFrame("StartPoint" => [1, 2], param => [3, 4])
2×2 DataFrame
 Row │ StartPoint  newvar 
     │ Int64       Int64  
─────┼────────────────────
   1 │          1       3
   2 │          2       4
6 Likes

Thanks for the replies, everyone.