将单个数字添加到 julia 数据框中的列?

The*_*Cat 4 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 ?
???????????????????????????????????????????????????????????????????????
? 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
Run Code Online (Sandbox Code Playgroud)

Bog*_*ski 6

您可以使用.广播:

iris.SepalLength .+= 5
Run Code Online (Sandbox Code Playgroud)

  • 我现在正在更新 DataFrames.jl 的文档,但这是一项艰巨的任务。会注意添加它。 (2认同)