[英]pyspark join with null conditions
我正在嘗試根據“年份”和“發票”列加入兩個 pyspark 數據框,如下所示。 但是如果df1中缺少“年份”,那么我只需要根據“發票”就可以加入
df1:
Year invoice Status Item
2020 262 YES bag
2019 252 YES ball
2018 240 YES pen
2017 228 YES ink
2016 216 NO headphone
2015 213 NO bicycle
198 NO ribbon
175 YES phone
df2:
Year invoice
2020 262
2016 216
2014 175
2013 198
2019 252
預期輸出:
Year invoice Status Item
2020 262 YES bag
2016 216 NO headphone
2014 175 YES phone
2013 198 NO ribbon
2019 252 YES ball
我可以按如下方式加入 df1 和 df2(僅基於年份和發票”列。如果 df1 中缺少年份,我需要添加僅基於發票加入兩列的邏輯。
df_results = df1.join(df2, on=['Year', 'invoice'], how='left') \
.drop(df2.Year) \
.drop(df2.invoice)
如果“年份”在 df1 中不可用,請告訴我如何加入,並且應僅根據“發票”加入數據框。 謝謝。
我沒有你的代碼來測試這個,但我會嘗試向連接操作添加一個條件:
cond = ((df1.Year == df2.Year) | df1.Year.isNull()) & (df1.invoice == df2.invoice)
df_results = df1.join(df2, on=cond, how='left') \
.drop(df2.Year) \
.drop(df2.invoice)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.