Dataframe 如何替换管道中缺少的值

Dataframe 如何替换管道中缺少的值,dataframe,julia,Dataframe,Julia,我想替换管道中缺少的值。我知道怎么在管道外面做 我试图编写一个转换函数,但是ismissing()在这种情况下不起作用。我假设这是因为它得到了一列,而不是单独的值。但是正在消失。(也不起作用。有人知道我怎样才能使这种转变起作用吗 julia> ismissing(df.a[1]) true julia> @pipe df |> DataFrames.transform(_, :a => x -> ismissing(x) ? "

我想替换管道中缺少的值。我知道怎么在管道外面做

我试图编写一个转换函数,但是
ismissing()
在这种情况下不起作用。我假设这是因为它得到了一列,而不是单独的值。但是
正在消失。(
也不起作用。有人知道我怎样才能使这种转变起作用吗

julia> ismissing(df.a[1])
true

julia> @pipe df |>
             DataFrames.transform(_, :a => x -> ismissing(x) ? "control" : x)
2×3 DataFrame
│ Row │ b     │ a         │ a_function │
│     │ Int64 │ String?   │ String?    │
├─────┼───────┼───────────┼────────────┤
│ 1   │ 2     │ missing   │ missing    │
│ 2   │ 3     │ treatment │ treatment  │

julia> @pipe df |>
             DataFrames.transform(_, :a => x -> ismissing(x) ? x : "control" )
2×3 DataFrame
│ Row │ b     │ a         │ a_function │
│     │ Int64 │ String?   │ String     │
├─────┼───────┼───────────┼────────────┤
│ 1   │ 2     │ missing   │ control    │
│ 2   │ 3     │ treatment │ control    │

julia> @pipe df |>
             DataFrames.transform(_, :a => (x -> ismissing.(x) ? "control" : x) )
ERROR: TypeError: non-boolean (BitArray{1}) used in boolean context
附言。 我知道我可以使用
@transform
宏,但我觉得它不是很优雅。我认为这种替换应该可以在一行代码中实现


julia> @pipe df |>
             @transform(_, :a, x = replace(:a, missing => "control")) |>
             select(_, Not(:a)) |>
             rename(_, :x => :a)
2×2 DataFrame
│ Row │ b     │ a         │
│     │ Int64 │ String    │
├─────┼───────┼───────────┤
│ 1   │ 2     │ control   │
│ 2   │ 3     │ treatment │

这里有四种示例方法

julia> @pipe df |>
             select(_, :a => ByRow(x -> coalesce(x, "control")) => :a, :b)
2×2 DataFrame
 Row │ a          b
     │ String     Int64
─────┼──────────────────
   1 │ control        2
   2 │ treatment      3

julia> @pipe df |>
             select(_, :a => (x -> coalesce.(x, "control")) => :a, :b)
2×2 DataFrame
 Row │ a          b
     │ String     Int64
─────┼──────────────────
   1 │ control        2
   2 │ treatment      3

julia> @pipe df |>
             select(_, :a => (x -> replace(x, missing => "control")) => :a, :b)
2×2 DataFrame
 Row │ a          b
     │ String     Int64
─────┼──────────────────
   1 │ control        2
   2 │ treatment      3

julia> @pipe df |>
             select(_, :a => ByRow(x -> ismissing(x) ? "control" : x) => :a, :b)
2×2 DataFrame
 Row │ a          b
     │ String     Int64
─────┼──────────────────
   1 │ control        2
   2 │ treatment      3
julia> @pipe df |>
             select(_, :a => ByRow(x -> coalesce(x, "control")) => :a, :b)
2×2 DataFrame
 Row │ a          b
     │ String     Int64
─────┼──────────────────
   1 │ control        2
   2 │ treatment      3

julia> @pipe df |>
             select(_, :a => (x -> coalesce.(x, "control")) => :a, :b)
2×2 DataFrame
 Row │ a          b
     │ String     Int64
─────┼──────────────────
   1 │ control        2
   2 │ treatment      3

julia> @pipe df |>
             select(_, :a => (x -> replace(x, missing => "control")) => :a, :b)
2×2 DataFrame
 Row │ a          b
     │ String     Int64
─────┼──────────────────
   1 │ control        2
   2 │ treatment      3

julia> @pipe df |>
             select(_, :a => ByRow(x -> ismissing(x) ? "control" : x) => :a, :b)
2×2 DataFrame
 Row │ a          b
     │ String     Int64
─────┼──────────────────
   1 │ control        2
   2 │ treatment      3