英文:
【Julia: DataFrame】 How to combine two lines into one using `|>`
问题
可以将#1
和#2
合并成一行,使用|>
操作符,如下所示:
cond = df.A .> 0 |> (x -> replace(x, missing => false))
英文:
Consider the following:
df = DataFrame(A=[-1,missing,1],B=[10,20,30])
3×2 DataFrame
Row │ A B
│ Int64? Int64
─────┼────────────────
1 │ -1 10
2 │ missing 20
3 │ 1 30
cond = df.A .> 0. #1
cond = replace(cond, missing => false) #2
df[cond,:]
Is it possible to combine #1
and #2
into one line using |>
like
cond = df.A .> 0 |> replace(missing => false)
which fails.
答案1
得分: 2
为什么不只是交换它们的顺序?
replace(df.A, missing => false) |>
x -> x > 0
# 或者简单点
replace(df.A, missing => false) .> 0
更新:使用 Chain.jl
:
using Chain
@chain df begin
_.A > 0
replace(_, missing => false)
end
英文:
Why not just swap the order of them?
replace(df.A, missing => false) |>
x-> x.> 0
# or simply
replace(df.A, missing => false) .> 0
Update: with Chain.jl
:
using Chain
@chain df begin
_.A .> 0
replace(_, missing => false)
end
答案2
得分: 2
基础支持中的管道仅支持具有一个参数的函数。当将管道与匿名函数结合使用时,它将起作用,其中 (df.A .> 0)
需要在 (
和 )
中。
using DataFrames
df = DataFrame(A=[-1,missing,1],B=[10,20,30])
cond = (df.A .> 0) |> x -> replace(x, missing => false)
cond
#3-element Vector{Bool}:
# 0
# 0
# 1
# 或者使用 coalesce。
cond = (df.A .> 0) |> x -> coalesce.(x, false)
另一种可能性是使用 Pipe.jl
包,需要使用 @pipe
并且占位符为 _
。
using Pipe: @pipe
cond = @pipe (df.A .> 0) |> replace(_, missing => false)
除了 Pipe.jl
,这也适用于 Hose.jl
。
using Hose
cond = @hose (df.A .> 0) |> replace(_, missing => false)
# 或者
cond = @hose (df.A .> 0) |> replace(missing => false)
或者使用 Plumber.jl
。
using Plumber
cond = @pipe (df.A .> 0) |> replace(_, missing => false)
# 或者
@pipe cond = (df.A .> 0) |> replace(_, missing => false)
或者使用 Chain.jl
。
using Chain
cond = @chain df.A .> 0 replace(_, missing => false)
# 或者
cond = @chain df.A .> 0 replace(missing => false)
或者使用 Lazy.jl
。
using Lazy
cond = @> df.A .> 0 replace(missing => false)
# 或者
cond = @as x df.A .> 0 replace(x, missing => false)
英文:
Pipes in Base support only functions with one argument. When combining the pipe with an anonymous function it will work, where (df.A .> 0)
needs to be in (
and )
using DataFrames
df = DataFrame(A=[-1,missing,1],B=[10,20,30])
cond = (df.A .> 0) |> x -> replace(x, missing => false)
cond
#3-element Vector{Bool}:
# 0
# 0
# 1
# Or using coalesce.
cond = (df.A .> 0) |> x -> coalesce.(x, false)
Another possibility will be to use the package Pipe.jl
, where @pipe
needs to be used and the placeholder is _
.
using Pipe: @pipe
cond = @pipe (df.A .> 0) |> replace(_, missing => false)
Beside Pipe.jl
this works also with Hose.jl
using Hose
cond = @hose (df.A .> 0) |> replace(_, missing => false)
#or
cond = @hose (df.A .> 0) |> replace(missing => false)
or with Plumber.jl
using Plumber
cond = @pipe (df.A .> 0) |> replace(_, missing => false)
#or
@pipe cond = (df.A .> 0) |> replace(_, missing => false)
or with Chain.jl
using Chain
cond = @chain df.A .> 0 replace(_, missing => false)
#or
cond = @chain df.A .> 0 replace(missing => false)
or with Lazy.jl
using Lazy
cond = @> df.A .> 0 replace(missing => false)
#or
cond = @as x df.A .> 0 replace(x, missing => false)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论