[英]PySpark join DataFrames multiple columns dynamically ('or' operator)
我有一個場景,我需要動態加入兩個 DataFrame。 我正在創建一個輔助函數並將數據幀作為輸入參數傳遞,如下所示。
def joinDataFrame(first_df, second_df, first_cols, second_cols,join_type) -> DataFrame:
return_df = first_df.join(second_df, (col(f) == col(s) for (f,s) in zip(first_cols, second_cols), join_type)
return return_df
如果我只有“和”場景,這很好用,但我也有通過“或”條件的要求。
我確實嘗試構建一個包含條件的字符串,然后使用expr()
我可以傳遞連接條件,但我得到了'ParseException'
。
我更願意構建“加入”條件並將其作為參數傳遞給此函數。
減少使用|
在壓縮平等條件下:
from functools import reduce
join_cond = reduce(
lambda x, y: x | y,
(first_df[f] == second_df[s] for (f,s) in zip(first_cols, second_cols))
)
return_df = first_df.join(second_df, join_cond, join_type)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.