How do I insert missing values to my dataframe in

2019-05-22 13:38发布

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

Even NA is not working.

I am getting an error

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

1条回答
smile是对你的礼貌
2楼-- · 2019-05-22 13:59

Use allowmissing! function.

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       │

You can see which columns in a DataFrame allow missing because they are highlighted with after type name under column name.

You can also use allowmissing function to create a new DataFrame.

Both functions optionally accept columns that are to be converted.

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).

查看更多
登录 后发表回答