朱莉娅|数据框|替换缺失值 [英] Julia | DataFrame | Replacing missing Values
问题描述
如何用0.0
替换DataFrame
中的列的missing
值?
How can we replace missing
values with 0.0
for a column in a DataFrame
?
推荐答案
有几种方法可以解决此问题(对Julia 1.x有效):
There are a few different approaches to this problem (valid for Julia 1.x):
最简单的方法可能是使用基本Julia中的replace!
或replace
.这是replace!
的示例:
Probably the easiest approach is to use replace!
or replace
from base Julia. Here is an example with replace!
:
julia> using DataFrames
julia> df = DataFrame(x = [1, missing, 3])
3×1 DataFrame
│ Row │ x │
│ │ Int64⍰ │
├─────┼─────────┤
│ 1 │ 1 │
│ 2 │ missing │
│ 3 │ 3 │
julia> replace!(df.x, missing => 0);
julia> df
3×1 DataFrame
│ Row │ x │
│ │ Int64⍰ │
├─────┼────────┤
│ 1 │ 1 │
│ 2 │ 0 │
│ 3 │ 3 │
但是,请注意,此时x
列的类型仍允许缺少值:
However, note that at this point the type of column x
still allows missing values:
julia> typeof(df.x)
Array{Union{Missing, Int64},1}
当打印出数据框时,在x
列中Int64
之后的问号也表明了这一点.您可以通过使用disallowmissing!
来更改此设置(
This is also indicated by the question mark following Int64
in column x
when the data frame is printed out. You can change this by using disallowmissing!
(from the DataFrames.jl package):
julia> disallowmissing!(df, :x)
3×1 DataFrame
│ Row │ x │
│ │ Int64 │
├─────┼───────┤
│ 1 │ 1 │
│ 2 │ 0 │
│ 3 │ 3 │
或者,如果您按如下方式使用replace
(不带感叹号),则输出将已经不允许缺少值:
Alternatively, if you use replace
(without the exclamation mark) as follows, then the output will already disallow missing values:
julia> df = DataFrame(x = [1, missing, 3]);
julia> df.x = replace(df.x, missing => 0);
julia> df
3×1 DataFrame
│ Row │ x │
│ │ Int64 │
├─────┼───────┤
│ 1 │ 1 │
│ 2 │ 0 │
│ 3 │ 3 │
具有逻辑索引的Base.ismissing
您可以将ismissing
与逻辑索引一起使用,为数组的所有缺失条目分配新值:
Base.ismissing with logical indexing
You can use ismissing
with logical indexing to assign a new value to all missing entries of an array:
julia> df = DataFrame(x = [1, missing, 3]);
julia> df.x[ismissing.(df.x)] .= 0;
julia> df
3×1 DataFrame
│ Row │ x │
│ │ Int64⍰ │
├─────┼────────┤
│ 1 │ 1 │
│ 2 │ 0 │
│ 3 │ 3 │
Base.coalesce
另一种方法是使用coalesce
:
julia> df = DataFrame(x = [1, missing, 3]);
julia> df.x = coalesce.(df.x, 0);
julia> df
3×1 DataFrame
│ Row │ x │
│ │ Int64 │
├─────┼───────┤
│ 1 │ 1 │
│ 2 │ 0 │
│ 3 │ 3 │
DataFramesMeta
replace
和coalesce
都可以与 DataFramesMeta中的@transform
宏一起使用. jl 软件包:
DataFramesMeta
Both replace
and coalesce
can be used with the @transform
macro from the DataFramesMeta.jl package:
julia> using DataFramesMeta
julia> df = DataFrame(x = [1, missing, 3]);
julia> @transform(df, x = replace(:x, missing => 0))
3×1 DataFrame
│ Row │ x │
│ │ Int64 │
├─────┼───────┤
│ 1 │ 1 │
│ 2 │ 0 │
│ 3 │ 3 │
julia> df = DataFrame(x = [1, missing, 3]);
julia> @transform(df, x = coalesce.(:x, 0))
3×1 DataFrame
│ Row │ x │
│ │ Int64 │
├─────┼───────┤
│ 1 │ 1 │
│ 2 │ 0 │
│ 3 │ 3 │
其他文档
- Julia手册
- Julia手册-函数参考
- DataFrames.jl手册
- Julia manual
- Julia manual - function reference
- DataFrames.jl manual
Additional documentation
这篇关于朱莉娅|数据框|替换缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!