向julia数据帧中的列添加单个数字?

向julia数据帧中的列添加单个数字?,julia,Julia,我想将数字5添加到julia数据帧中的列中。我该怎么做 julia> using DataFrames, CSV julia> iris = CSV.read(joinpath(Pkg.dir("DataFrames"), "test/data/iris.csv")); julia> head(iris) 6×5 DataFrame │ Row │ SepalLength │ SepalWidth │ PetalLength │ PetalWidth │ Species │

我想将数字5添加到julia数据帧中的列中。我该怎么做

julia> using DataFrames, CSV

julia> iris = CSV.read(joinpath(Pkg.dir("DataFrames"), "test/data/iris.csv"));

julia> head(iris)
6×5 DataFrame
│ Row │ SepalLength │ SepalWidth │ PetalLength │ PetalWidth │ Species │
├─────┼─────────────┼────────────┼─────────────┼────────────┼─────────┤
│ 1   │ 5.1         │ 3.5        │ 1.4         │ 0.2        │ setosa  │
│ 2   │ 4.9         │ 3.0        │ 1.4         │ 0.2        │ setosa  │
│ 3   │ 4.7         │ 3.2        │ 1.3         │ 0.2        │ setosa  │
│ 4   │ 4.6         │ 3.1        │ 1.5         │ 0.2        │ setosa  │
│ 5   │ 5.0         │ 3.6        │ 1.4         │ 0.2        │ setosa  │
│ 6   │ 5.4         │ 3.9        │ 1.7         │ 0.4        │ setosa  │

julia> iris[:SepalLength] += 5

ERROR: MethodError: no method matching +(::Array{Union{Missing, Float64},1}, ::Int64)
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:502
  +(::Complex{Bool}, ::Real) at complex.jl:292
  +(::Missing, ::Number) at missing.jl:93
  ...
Stacktrace:
 [1] top-level scope at none:0

julia> map(iris[2], x -> x + 5)
ERROR: MethodError: no method matching iterate(::getfield(Main, Symbol("##33#34")))
Closest candidates are:
  iterate(::Core.SimpleVector) at essentials.jl:578
  iterate(::Core.SimpleVector, ::Any) at essentials.jl:578
  iterate(::ExponentialBackOff) at error.jl:171
  ...
Stacktrace:
 [1] iterate at ./generator.jl:44 [inlined]
 [2] collect(::Base.Generator{getfield(Main, Symbol("##33#34")),Array{Union{Missing, Float64},1}}) at ./array.jl:619
 [3] map(::Array{Union{Missing, Float64},1}, ::Function) at ./abstractarray.jl:2013
 [4] top-level scope at none:0

您可以使用
广播:

iris.SepalLength .+= 5

我现在正在更新DataFrames.jl的文档,但这是一项艰巨的任务。请注意添加它。是否有任何一个线性行可以对数据帧的所有列进行相同的扩展,或者我需要一个循环来完成此操作?目前,在一个线性行中,最好通过一个矩阵:
dataframe(矩阵(df)+fill(5,大小(df)…)
,其中
df
是您的
dataframe
。这将创建一个新的数据帧。