gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:22:20 26 4
gpt4 key购买 nike

我想将数字 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

关于julia - 将单个数字添加到 julia 数据框中的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52423850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com