Julia DataFrame 用 LOCF 填充 NA [英] Julia DataFrame Fill NA with LOCF
本文介绍了Julia DataFrame 用 LOCF 填充 NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有什么快速的方法可以将 DataFrame 的 NA 值转换为最后观察到的值?
Is there any fast way to convert a DataFrame's NA values to the last observed value?
using DataFrames
d = @data [1,NA,5,NA,NA]
df = DataFrame(d=d)
result = filled_with_locf(df)
expected = [1,1,5,5,5]
- LOCF = 最后一次观察结转
推荐答案
扩展注释 oneliner,如果我们定义 locf
为:
Expanding on the comment oneliner, if we define locf
as:
locf(v) = v[cummax([i*!isna(v[i]) for i=1:length(v)])]
那么,
nona_df = DataFrame(Any[locf(df[c]) for c in names(df)],names(df))
还有,
julia> nona_df
5×1 DataFrames.DataFrame
│ Row │ d │
├─────┼───┤
│ 1 │ 1 │
│ 2 │ 1 │
│ 3 │ 5 │
│ 4 │ 5 │
│ 5 │ 5 │
这篇关于Julia DataFrame 用 LOCF 填充 NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文