[英]Join 2 Dataframes with Regex in where clause pyspark
我們有兩個數據框
df = spark.createDataFrame([
(1, 'Nick', 'Miller'),
(2, 'Jessica', 'Day'),
(3, 'Winston', 'Schmidt'),
], ['id', 'First_name', 'Last_name'])
df1 = spark.createDataFrame([ (1, '^[a-lA-L]', 'type1'), (3, '^[m-zM-Z]', 'type2')], ['id', 'regex_match', 'vaule']
需要加入這兩個dataframe,其中df1.regex_match
與df.Last_name
匹配
需要 output 如下: 任何建議請:
join df to df1 using left join
您可以使用rlike
條件加入:
import pyspark.sql.functions as F
result = df.alias('df').join(
df1.drop('id').alias('df1'),
F.expr('df.Last_name rlike df1.regex_match'),
'left'
).drop('regex_match')
result.show()
+---+----------+---------+-----+
| id|First_name|Last_name|vaule|
+---+----------+---------+-----+
| 1| Nick| Miller|type2|
| 2| Jessica| Day|type1|
| 3| Winston| Schmidt|type2|
+---+----------+---------+-----+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.