For question 1, you may want below result:
julia> df = DataFrame(a = [-1, missing,missing,3], b = [4,5,missing, 7.2], c = ["k", missing, "m", "asd"])
4×3 DataFrame
Row │ a b c
│ Int64? Float64? String?
─────┼─────────────────────────────
1 │ -1 4.0 k
2 │ missing 5.0 missing
3 │ missing missing m
4 │ 3 7.2 asd
julia> for col ∈ eachcol(df)
if String <: eltype(col)
col[ismissing.(col)] .= "xyz"
else
col[ismissing.(col)] .= 0
end
end
julia> df
4×3 DataFrame
Row │ a b c
│ Int64? Float64? String?
─────┼───────────────────────────
1 │ -1 4.0 k
2 │ 0 5.0 xyz
3 │ 0 0.0 m
4 │ 3 7.2 asd
Code:
using DataFrames
df = DataFrame(a = [-1, missing,missing,3], b = [4,5,missing, 7.2], c = ["k", missing, "m", "asd"])
for col ∈ eachcol(df)
if String <: eltype(col)
col[ismissing.(col)] .= "xyz"
else
col[ismissing.(col)] .= 0
end
end
df
For question 2, well, I don’t know it’s efficient or short, but ifelse
function solves almost cases.
julia> df = DataFrame(a = [-1, missing,missing,3], b = [4,5,missing, 7.2], c = ["k", missing, "m", "asd"])
4×3 DataFrame
Row │ a b c
│ Int64? Float64? String?
─────┼─────────────────────────────
1 │ -1 4.0 k
2 │ missing 5.0 missing
3 │ missing missing m
4 │ 3 7.2 asd
julia> df[!, :a] = ifelse.(ismissing.(df[!, :a]), df[!, :a], string.(df[!, :a]))
4-element Vector{Union{Missing, String}}:
"-1"
missing
missing
"3"
julia> df
4×3 DataFrame
Row │ a b c
│ String? Float64? String?
─────┼─────────────────────────────
1 │ -1 4.0 k
2 │ missing 5.0 missing
3 │ missing missing m
4 │ 3