当连接键以列表形式给出时,如何修改spark数据帧中连接的列?

2024-04-19 02:38:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用以下作为列表传递的join键列表来连接两个dataframe,我想添加功能,以便在其中一个键值为null时连接键的子集

我一直在尝试连接两个数据帧dfu 1和dfu 2。你知道吗

data1 = [[1,'2018-07-31',215,'a'],
        [2,'2018-07-30',None,'b'],
        [3,'2017-10-28',201,'c']
     ]
df_1 = sqlCtx.createDataFrame(data1, 
['application_number','application_dt','account_id','var1']) 

以及

data2 = [[1,'2018-07-31',215,'aaa'],
        [2,'2018-07-30',None,'bbb'],
        [3,'2017-10-28',201,'ccc']
        ]
df_2 = sqlCtx.createDataFrame(data2, 
['application_number','application_dt','account_id','var2'])

我用来连接的代码是:

key_a = ['application_number','application_dt','account_id']
new = df_1.join(df_2,key_a,'left')

其输出为:

+------------------+--------------+----------+----+----+
|application_number|application_dt|account_id|var1|var2|
+------------------+--------------+----------+----+----+
|                 1|    2018-07-31|       215|   a| aaa|
|                 3|    2017-10-28|       201|   c| ccc|
|                 2|    2018-07-30|      null|   b|null|
+------------------+--------------+----------+----+----+

我在这里关心的是,在account\u id为null的情况下,连接仍然应该通过比较其他两个键来工作。你知道吗

所需的输出应如下所示:

+------------------+--------------+----------+----+----+
|application_number|application_dt|account_id|var1|var2|
+------------------+--------------+----------+----+----+
|                 1|    2018-07-31|       215|   a| aaa|
|                 3|    2017-10-28|       201|   c| ccc|
|                 2|    2018-07-30|      null|   b| bbb|
+------------------+--------------+----------+----+----+

我发现了一种类似的方法,即使用以下语句:

  join_elem = "df_1.application_number == 
  df_2.application_number|df_1.application_dt == 
  df_2.application_dt|F.coalesce(df_1.account_id,F.lit(0)) ==  
  F.coalesce(df_2.account_id,F.lit(0))".split("|")
  join_elem_column = [eval(x) for x in join_elem]

但是,出于设计考虑,我不允许使用完整的联接表达式,我只能使用列名列表作为联接键。你知道吗

我一直试图找到一种方法来适应这个合并到这个清单本身的事情,但迄今为止还没有发现任何成功。你知道吗


Tags: idnumberdf列表applicationdtaccountnull
1条回答
网友
1楼 · 发布于 2024-04-19 02:38:15

我把这个解决方案称为变通方法。你知道吗

这里的问题是,对于DataFrame中的一个键,我们有Null值,OP希望使用其余的键列。为什么不给这个Null赋一个任意值,然后应用连接呢。实际上,这和在剩下的两个键上进行连接是一样的。你知道吗

# Let's replace Null with an arbitrary value, which has
# little chance of occurring in the Dataset. For eg; -100000
df1 = df1.withColumn('account_id', when(col('account_id').isNull(),-100000).otherwise(col('account_id')))    
df2 = df2.withColumn('account_id', when(col('account_id').isNull(),-100000).otherwise(col('account_id')))

# Do a FULL Join
df = df1.join(df2,['application_number','application_dt','account_id'],'full')

# Replace the arbitrary value back with Null.    
df = df.withColumn('account_id', when(col('account_id')== -100000, None).otherwise(col('account_id')))
df.show()
+         +       +     +  +  +
|application_number|application_dt|account_id|var1|var2|
+         +       +     +  +  +
|                 1|    2018-07-31|       215|   a| aaa|
|                 2|    2018-07-30|      null|   b| bbb|
|                 3|    2017-10-28|       201|   c| ccc|
+         +       +     +  +  +

相关问题 更多 >