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

时间:2018-09-20 11:13:44

标签: 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

1 个答案:

答案 0 :(得分:4)

您可以使用.广播:

iris[:SepalLength] .+= 5