如何在 Julia 中将缺失值插入我的数据框中 [英] How do I insert missing values to my dataframe in Julia
问题描述
df3[10, :A] = missing
df3[15, :B] = missing
df3[15, :C] = missing
即使 NA 也不起作用.
Even NA is not working.
我遇到了一个错误
MethodError: 无法将 Missings.Missing 类型的对象 convert
转换为 Int64 类型的对象这可能源于对构造函数 Int64(...) 的调用,因为类型构造函数回退到转换方法.堆栈跟踪:[1] setindex!(::Array{Int64,1}, ::Missings.Missing, ::Int64) 在 ./array.jl:583[2] insert_single_entry!(::DataFrames.DataFrame, ::Missings.Missing, ::Int64, ::Symbol) 在/home/jrun/.julia/v0.6/DataFrames/src/dataframe/dataframe.jl:361[3] setindex!(::DataFrames.DataFrame, ::Missings.Missing, ::Int64, ::Symbol) 在/home/jrun/.julia/v0.6/DataFrames/src/dataframe/dataframe.jl:448[4] include_string(::String, ::String) at ./loading.jl:522
MethodError: Cannot
convert
an object of type Missings.Missing to an object of type Int64 This may have arisen from a call to the constructor Int64(...), since type constructors fall back to convert methods. Stacktrace: [1] setindex!(::Array{Int64,1}, ::Missings.Missing, ::Int64) at ./array.jl:583 [2] insert_single_entry!(::DataFrames.DataFrame, ::Missings.Missing, ::Int64, ::Symbol) at /home/jrun/.julia/v0.6/DataFrames/src/dataframe/dataframe.jl:361 [3] setindex!(::DataFrames.DataFrame, ::Missings.Missing, ::Int64, ::Symbol) at /home/jrun/.julia/v0.6/DataFrames/src/dataframe/dataframe.jl:448 [4] include_string(::String, ::String) at ./loading.jl:522
推荐答案
使用allowmissing!
函数.
julia> using DataFrames
julia> df = DataFrame(a=[1,2,3])
3×1 DataFrame
│ Row │ a │
│ │ Int64 │
├─────┼───────┤
│ 1 │ 1 │
│ 2 │ 2 │
│ 3 │ 3 │
julia> df.a[1] = missing
ERROR: MethodError: Cannot `convert` an object of type Missing to an object of type Int64
julia> allowmissing!(df)
3×1 DataFrame
│ Row │ a │
│ │ Int64⍰ │
├─────┼────────┤
│ 1 │ 1 │
│ 2 │ 2 │
│ 3 │ 3 │
julia> df.a[1] = missing
missing
julia> df
3×1 DataFrame
│ Row │ a │
│ │ Int64⍰ │
├─────┼─────────┤
│ 1 │ missing │
│ 2 │ 2 │
│ 3 │ 3 │
您可以查看 DataFrame
中的哪些列允许 missing
,因为它们在列名下的类型名称后用 ⍰
突出显示.
You can see which columns in a DataFrame
allow missing
because they are highlighted with ⍰
after type name under column name.
你也可以使用allowmissing
函数来创建一个新的DataFrame
.
You can also use allowmissing
function to create a new DataFrame
.
这两个函数都可选择接受要转换的列.
Both functions optionally accept columns that are to be converted.
最后有一个 disallowmissing
/disallowmissing!
对可以做相反的事情(即从 eltype中去除可选的
Missing
联合code> 如果向量实际上不包含缺失值).
Finally there is a disallowmissing
/disallowmissing!
pair that does the reverse (i.e. strips optional Missing
union from eltype
if a vector actually contains no missing values).
这篇关于如何在 Julia 中将缺失值插入我的数据框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!