Hello,
I have a matrix features
2 columns 2000 rows and I would like to convert it into a data frame.
I tried with:
julia> df = DataFrames(Time = features[:,1], Amount = features[:,2])
ERROR: MethodError: objects of type Module are not callable
Stacktrace:
[1] top-level scope
@ none:1
How do I create a data frame from vectors in Julia?
PS: to note that
julia> using DataFrames
julia> df = Dataframe()
ERROR: UndefVarError: Dataframe not defined
Stacktrace:
[1] top-level scope
@ none:1
DataFrame
julia> features = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> df = DataFrame(Time=features[:,1], Amount=features[:,2])
2×2 DataFrame
Row │ Time Amount
│ Int64 Int64
─────┼───────────────
1 │ 1 2
2 │ 3 4
2 Likes
so it was the extra ‘s’. Thanks
Addendum: if I wanted to add a column, I understand I can use df.newColumn. I have an object that I believe is a vector, but when I add it to the data frame I get the repetition of the vector on each row of the dataframe:
julia> O
"[active, passive, passive, delayed_active, active, active, active, passive, passive, active, active, passive, active, ### to note that REPL does not say "vector"
julia> df.Outcome = O
ERROR: ArgumentError: It is only allowed to pass a vector as a column of a DataFrame. Instead use `df[!, col_ind] .= v` if you want to use broadcasting.
Stacktrace:
[1] setproperty!(#unused#::DataFrame, col_ind::Symbol, v::String)
@ DataFrames ~/.julia/packages/DataFrames/ORSVA/src/dataframe/dataframe.jl:650
[2] top-level scope
@ none:1
julia> df.Outcome = [O]
ERROR: ArgumentError: New columns must have the same length as old columns
Stacktrace:
[1] insert_single_column!(df::DataFrame, v::Vector{String}, col_ind::Symbol)
@ DataFrames ~/.julia/packages/DataFrames/ORSVA/src/dataframe/dataframe.jl:610
[2] setindex!
@ ~/.julia/packages/DataFrames/ORSVA/src/dataframe/dataframe.jl:640 [inlined]
[3] setproperty!(df::DataFrame, col_ind::Symbol, v::Vector{String})
@ DataFrames ~/.julia/packages/DataFrames/ORSVA/src/dataframe/dataframe.jl:646
[4] top-level scope
@ none:1
julia> df.[!, Outcome] .= O
ERROR: MethodError: no method matching getproperty(::DataFrame, ::Expr)
Closest candidates are:
getproperty(::AbstractDataFrame, ::AbstractString) at ~/.julia/packages/DataFrames/ORSVA/src/abstractdataframe/abstractdataframe.jl:379
getproperty(::AbstractDataFrame, ::Symbol) at ~/.julia/packages/DataFrames/ORSVA/src/abstractdataframe/abstractdataframe.jl:378
getproperty(::Any, ::Symbol) at ~/src/julia/share/julia/base/Base.jl:42
...
Stacktrace:
[1] dotgetproperty(x::DataFrame, f::Expr)
@ Base ./Base.jl:45
[2] top-level scope
@ none:1
I think here the problem is that O is not a vector (although it came from one) and I need to split O at the commas…
Yes, solved it with O = split(O, ",")