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

问题描述

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

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

Even NA is not working.

我遇到错误

推荐答案

使用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中向我的数据框插入缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 02:20