代码之家  ›  专栏  ›  技术社区  ›  The Unfun Cat

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

  •  2
  • The Unfun Cat  · 技术社区  · 6 年前

    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 回复  |  直到 6 年前
        1
  •  6
  •   Bogumił Kamiński    4 年前

    .

    iris.SepalLength .+= 5