I want to append a vector as a column to an empty DataFrame
. Suppose I have defined an empty DataFrame
like this:
import DataFrames
dataframe = DataFrames.DataFrame()
Then I want to append this vector as a column to the dataframe
:
vec = [1,2,3]
I tried push!(dataframe , vec)
, but I got this error:
DimensionMismatch("Length of `row` does not match `DataFrame` column count.")
Stacktrace:
[1] push!(df::DataFrames.DataFrame, row::Vector{Int64}; promote::Bool)
@ DataFrames C:\Users\Shayan\.julia\packages\DataFrames\BM4OQ\src\dataframe\dataframe.jl:1691
[2] push!(df::DataFrames.DataFrame, row::Vector{Int64})
@ DataFrames C:\Users\Shayan\.julia\packages\DataFrames\BM4OQ\src\dataframe\dataframe.jl:1680
[3] top-level scope
@ c:\Users\Shayan\Documents\PyJul Scripts\Jul-test.ipynb:2
[4] eval
@ .\boot.jl:373 [inlined]
[5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1196
[6] #invokelatest#2
@ .\essentials.jl:716 [inlined]
[7] invokelatest
@ .\essentials.jl:714 [inlined]
[8] (::VSCodeServer.var"#164#165"{VSCodeServer.NotebookRunCellArguments, String})()
@ VSCodeServer c:\Users\Shayan\.vscode\extensions\julialang.language-julia-1.6.17\scripts\packages\VSCodeServer\src\serve_notebook.jl:19
[9] withpath(f::VSCodeServer.var"#164#165"{VSCodeServer.NotebookRunCellArguments, String}, path::String)
@ VSCodeServer c:\Users\Shayan\.vscode\extensions\julialang.language-julia-1.6.17\scripts\packages\VSCodeServer\src\repl.jl:184
[10] notebook_runcell_request(conn::VSCodeServer.JSONRPC.JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}, params::VSCodeServer.NotebookRunCellArguments)
@ VSCodeServer c:\Users\Shayan\.vscode\extensions\julialang.language-julia-1.6.17\scripts\packages\VSCodeServer\src\serve_notebook.jl:13
[11] dispatch_msg(x::VSCodeServer.JSONRPC.JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}, dispatcher::VSCodeServer.JSONRPC.MsgDispatcher, msg::Dict{String, Any})
@ VSCodeServer.JSONRPC c:\Users\Shayan\.vscode\extensions\julialang.language-julia-1.6.17\scripts\packages\JSONRPC\src\typed.jl:67
[12] serve_notebook(pipename::String, outputchannel_logger::Base.CoreLogging.SimpleLogger; crashreporting_pipename::String)
@ VSCodeServer c:\Users\Shayan\.vscode\extensions\julialang.language-julia-1.6.17\scripts\packages\VSCodeServer\src\serve_notebook.jl:136
[13] top-level scope
@ c:\Users\Shayan\.vscode\extensions\julialang.language-julia-1.6.17\scripts\notebook\notebook.jl:32
[14] include(mod::Module, _path::String)
@ Base .\Base.jl:418
[15] exec_options(opts::Base.JLOptions)
@ Base .\client.jl:292
[16] _start()
@ Base .\client.jl:495
Also, I tried insert!(dataframe , vec)
, but I got this:
MethodError: no method matching insert!(::DataFrames.DataFrame, ::Vector{Int64})
Closest candidates are:
insert!(!Matched::DataStructures.AVLTree{K}, ::K) where K at C:\Users\Shayan\.julia\packages\DataStructures\vSp4s\src\avl_tree.jl:128
insert!(!Matched::DataStructures.SortedSet, ::Any) at C:\Users\Shayan\.julia\packages\DataStructures\vSp4s\src\sorted_set.jl:114
insert!(!Matched::DataStructures.SortedDict{K, D, Ord}, ::Any, !Matched::Any) where {K, D, Ord<:Base.Order.Ordering} at C:\Users\Shayan\.julia\packages\DataStructures\vSp4s\src\sorted_dict.jl:268
How can I do this? Any help would be appreciated.
Additional Note: vec
is not defined before dataframe
and is intended! I mean, I have to create an empty DataFrame first!
There are the following options depending what you need.
or