左连接的关键字参数或解决方法,以便列的值被另一个 DataFrame 的值替换

问题描述 投票:0回答:2

什么是关键字参数或在

x
中连接 DataFrames
y
leftjoin!()
的解决方法,以便将
d
中的
x
列的值替换为
d
中的
y
的值返回错误?

using DataFrames
x=DataFrame(a=[1,2,3],b=[4,5,6],c=[7,20,9],d=[nothing,99,nothing])
y=DataFrame(a=[1,2,3],b=[4,5,6],c=[7,8,9],d=[10,11,12])
leftjoin!(x,y,on=[:a,:b,:c]) # returns error

所需输出:

3×4 DataFrame
 Row │ a      b      c      d
     │ Int64  Int64  Int64  Int64
─────┼────────────────────────────
   1 │     1      4      7     10
   2 │     2      5     20     99
   3 │     3      6      9     12
dataframe join error-handling julia left-join
2个回答
1
投票
select(
  leftjoin!(x,y,on=[:a,:b,:c]; makeunique=true),
  Not(r"d"),:d_1 => identity => :d)

d
列并不完美,因为它选择了
Missing
类型。

另一种选择:

combine(
  groupby(vcat(x,y),[:a,:b,:c]),
  :d => splat(something) =>:d)

添加:

根据评论,修复第一种方法,给出:

select(
  leftjoin(x,y,on=[:a,:b,:c]; makeunique=true),
  Not(r"d"),r"d" => ByRow(something) => :d)

0
投票
select(
    leftjoin(x, y, on=[:a, :b], makeunique=true), 
    :a, :b, ([col, "$(col)_1"] => ByRow(something) => col for col in ("c", "d"))...
)
© www.soinside.com 2019 - 2024. All rights reserved.