Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: Within data frame convert missing to 0

Tags:

julia

In R we can convert NA to 0 with:

df[is.na(df)] <- 0

This works for single columns:

df[ismissing.(df[:col]), :col] = 0

There a way for the full df?

like image 941
Andrew Bannerman Avatar asked Jun 22 '26 08:06

Andrew Bannerman


2 Answers

I don't think there's such a function in DataFrames.jl yet. But you can hack your way around it by combining colwise and recode. I'm also providing a reproducible example here, in case someone wants to iterate on this answer:

julia> using DataFrames

julia> df = DataFrame(a = [missing, 5, 5],
           b = [1, missing, missing])
3×2 DataFrames.DataFrame
│ Row │ a       │ b       │
├─────┼─────────┼─────────┤
│ 1   │ missing │ 1       │
│ 2   │ 5       │ missing │
│ 3   │ 5       │ missing │

julia> DataFrame(colwise(col -> recode(col, missing=>0), df), names(df))
3×2 DataFrames.DataFrame
│ Row │ a │ b │
├─────┼───┼───┤
│ 1   │ 0 │ 1 │
│ 2   │ 5 │ 0 │
│ 3   │ 5 │ 0 │

This is a bit ugly as you have to reassign the dataframe column names.

like image 129
niczky12 Avatar answered Jun 26 '26 21:06

niczky12


Maybe a simpler way to convert all missing values in a DataFrame is to just use list comprehension:

[df[ismissing.(df[i]), i] = 0 for i in names(df)]

like image 27
Antonello Avatar answered Jun 26 '26 22:06

Antonello