Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace missing values with previous values in Julia Data Frame

Tags:

julia

Imagine I have a data frame like below:

enter image description here

What I want to do is to fill those missing values with previous values, so after fillling the data frame would be like:

enter image description here

Is there any simple way that I can do this?

like image 946
JAKE Avatar asked Jun 12 '26 23:06

JAKE


1 Answers

This is the way to do it using Impute.jl:

julia> using Impute, DataFrames

julia> df = DataFrame(dt1=[0.2, missing, missing, 1, missing, 5, 6],
                      dt2=[0.3, missing, missing, 3, missing, 5, 6])
7×2 DataFrame
 Row │ dt1        dt2
     │ Float64?   Float64?
─────┼──────────────────────
   1 │       0.2        0.3
   2 │ missing    missing
   3 │ missing    missing
   4 │       1.0        3.0
   5 │ missing    missing
   6 │       5.0        5.0
   7 │       6.0        6.0

julia> transform(df, names(df) .=> Impute.locf, renamecols=false)
7×2 DataFrame
 Row │ dt1       dt2
     │ Float64?  Float64?
─────┼────────────────────
   1 │      0.2       0.3
   2 │      0.2       0.3
   3 │      0.2       0.3
   4 │      1.0       3.0
   5 │      1.0       3.0
   6 │      5.0       5.0
   7 │      6.0       6.0
like image 156
Bogumił Kamiński Avatar answered Jun 17 '26 10:06

Bogumił Kamiński