New to pyspark and any help would be appreciated:
Here's my use case:
df_1:
name partial_code
John 123
Mary 789
df_2:
complete_code
12345
12378
12300
78909
Expectation:
based on the partial_code in df_1, find all prefix matched results in df_2 and explode on it.
Example:
for John 123, since 12345, 12378, 12300 all prefix matched with 123, so the final result should be:
name code
John 12345
John 12378
John 12300
My approach
class Script:
getCodeUDF = udf(lambda x, y: Script.getCodes(x, y), ArrayType(StringType()))
df_final = df_1.withColumn("code", explode(getCodeUDF(df_2, "partial_code"))).drop("partial_code")
@staticmethod
def getCodes(df_2, codePrefix):
return df_2.filter(col("complete_code").startswith(codePrefix)).select("complete_code")
But it does not seem like I can pass a df as argument to UDF function, any other approach can solve this problem?