如何在Julia中向我的数据框插入缺失值 [英] How do I insert missing values to my dataframe in Julia

查看:260
本文介绍了如何在Julia中向我的数据框插入缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

df3[10, :A] = missing
df3[15, :B] = missing
df3[15, :C] = missing

即使是NA也无法正常工作.

Even NA is not working.

我遇到错误

MethodError:无法convert类型为Missings的对象.缺少到类型为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)位于./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并集).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆