【Julia: DataFrame】如何使用`|>`将两行合并为一行

huangapple go评论98阅读模式
英文:

【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)

huangapple
  • 本文由 发表于 2023年7月10日 17:33:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定