gpt4 book ai didi

dataframe - 在响应 dropna 的 julia DataFrames 中获取计算出的 NA 列值

转载 作者:行者123 更新时间:2023-12-03 08:09:17 25 4
gpt4 key购买 nike

我正在尝试使用 NA结果表明
给定数据帧“行”的计算值是无意义的
(或者可能无法计算)。我如何获得计算的列 NA仍然响应 dropna 的 s ?

例子:

using DataFrames

df = DataFrame(A = 1:4, B = [1, 0, 2, 3], C = [5, 4, 3, 3])

# A value of 0 in column B should yield a foo of NA
function foo(d)
if d[:B] == 0
return NA
end
return d[:B] ./ d[:C] # vectorized to work with `by`
end

# What I'm looking for is something equivalent to this list
# comprehension, but that returns a DataFrame or DataArray
# since normal Arrays don't respond to `dropna`

comprehension = [foo(frame) for frame in eachrow(df)]

最佳答案

一种选择是扩展 Base.convertDataArrays.dropna以便 dropna可以正常处理Vector s:

using DataFrames

function Base.convert{T}(::Type{DataArray}, v::Vector{T})
da = DataArray(T[],Bool[])
for val in v
push!(da, val)
end
return da
end

function DataArrays.dropna(v::Vector)
return dropna(convert(DataArray,v))
end

现在该示例应该按预期工作:
df = DataFrame(A = 1:4, B = [1, 0, 2, 3], C = [5, 4, 3, 3])

# A value of 0 in column B should yield a foo of NA
function foo(d)
if d[:B] == 0
return NA
end
return d[:B] / d[:C]
end

comprehension = [foo(frame) for frame in eachrow(df)]

dropna(comprehension) #=> Array{Any,1}: [0.2, 0.667, 1.]

即使没有扩展 dropna ,扩展 convert允许将推导作为新的 DataArray 列插入到 DataFrame 中,保留 NA s 及其适当的丢弃行为:
conv = convert(DataArray, comprehension)
insert!(df, size(df, 2) + 1, conv, :foo)
#=> 4x4 DataFrame
# | Row | A | B | C | foo |
# |-----|---|---|---|----------|
# | 1 | 1 | 1 | 5 | 0.2 |
# | 2 | 2 | 0 | 4 | NA |
# | 3 | 3 | 2 | 3 | 0.666667 |
# | 4 | 4 | 3 | 3 | 1.0 |

typeof(df[:foo]) #=> DataArray{Any,1} (constructor with 1 method)
dropna(df[:foo]) #=> Array{Any,1}: [0.2, 0.667, 1.]

关于dataframe - 在响应 dropna 的 julia DataFrames 中获取计算出的 NA 列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32644213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com