创建一个空的 DataFrame,然后用 Float64 值填充其中的一部分。

huangapple go评论80阅读模式
英文:

Julia: create empty DataFrame and then fill part of it with Float64 values

问题

以下是要翻译的内容:

我需要创建一个空的 DataFrame,然后只填充其中一些单元格的 Float64 值。
如果我在创建 DataFrame 时使用 "missing" 值来指示,然后当我想要插入值时就会出错。
如果我立即插入浮点值,就不会出现错误,但此时 DataFrame 不是空的。

也许问题是,如何立即将 DataFrame 转换为包含 "missing" 值?

最终结果应该如下所示: 创建一个空的 DataFrame,然后用 Float64 值填充其中的一部分。

英文:

I need to create empty DataFrame and later fill only some cells with Float64 values.
If I indicate with "missing" values while creating df, then I get error when I want to insert values.
I receive no error if I insert float values right away, but then my df is not empty.

Maybe the question is, how to convert df right away with "missing" values?

End result should be like this:
创建一个空的 DataFrame,然后用 Float64 值填充其中的一部分。

  1. > for i in 1:num_rows
  2. > col_name = string("c", "$i")
  3. > df[!, Symbol(col_name)] .= missing
  4. > end
  5. >
  6. > #After trying to insert values into specific cells:
  7. >
  8. > ERROR: MethodError: convert(::Type{Union{}}, ::Float64) is ambiguous. Candidates:
  9. > convert(::Type{T}, x::Number) where T<:Number in Base at number.jl:7
  10. > convert(::Type{T}, x::Number) where T<:AbstractChar in Base at char.jl:184
  11. > convert(::Type{Union{}}, x) in Base at essentials.jl:213
  12. > convert(::Type{T}, arg) where T<:VecElement in Base at baseext.jl:19
  13. > Possible fix, define
  14. > convert(::Type{Union{}}, ::Number)

答案1

得分: 2

  1. 5×1 DataFrame
  2. Row id
  3. Int64
  4. ─────┼───────
  5. 1 1
  6. 2 2
  7. 3 3
  8. 4 4
  9. 5 5
  10. julia> df.c1 = missings(Float64, nrow(df))
  11. 5-element Vector{Union{Missing, Float64}}:
  12. missing
  13. missing
  14. missing
  15. missing
  16. missing
  17. julia> df
  18. 5×2 DataFrame
  19. Row id c1
  20. Int64 Float64?
  21. ─────┼─────────────────
  22. 1 1 missing
  23. 2 2 missing
  24. 3 3 missing
  25. 4 4 missing
  26. 5 5 missing
  27. julia> df[2, :c1] = 12.5
  28. 12.5
  29. julia> df
  30. 5×2 DataFrame
  31. Row id c1
  32. Int64 Float64?
  33. ─────┼──────────────────
  34. 1 1 missing
  35. 2 2 12.5
  36. 3 3 missing
  37. 4 4 missing
  38. 5 5 missing
英文:

You most likely want this:

  1. julia> df = DataFrame(id=1:5)
  2. 5×1 DataFrame
  3. Row id
  4. Int64
  5. ─────┼───────
  6. 1 1
  7. 2 2
  8. 3 3
  9. 4 4
  10. 5 5
  11. julia> df.c1 = missings(Float64, nrow(df))
  12. 5-element Vector{Union{Missing, Float64}}:
  13. missing
  14. missing
  15. missing
  16. missing
  17. missing
  18. julia> df
  19. 5×2 DataFrame
  20. Row id c1
  21. Int64 Float64?
  22. ─────┼─────────────────
  23. 1 1 missing
  24. 2 2 missing
  25. 3 3 missing
  26. 4 4 missing
  27. 5 5 missing
  28. julia> df[2, :c1] = 12.5
  29. 12.5
  30. julia> df
  31. 5×2 DataFrame
  32. Row id c1
  33. Int64 Float64?
  34. ─────┼──────────────────
  35. 1 1 missing
  36. 2 2 12.5
  37. 3 3 missing
  38. 4 4 missing
  39. 5 5 missing

huangapple
  • 本文由 发表于 2023年3月3日 19:11:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定