我有两个PySpark DataFrame df1
和df2
:
df1
col1_1 col2_1 col3_1
1 A 12
2 B 13
3 C 14
4 D 15
5 E 16
df2
col1_2 col2_2 col3_2
1 A X
6 B Y
7 C Z
我想进行左外连接以获得此结果:
df_joined
col1_1 col2_1 col3_1 col3_2
1 A 12 X
2 B 13 Null
3 C 14 Null
4 D 15 Null
5 E 16 Null
因此,df_joined
的行数应与df1
相同。
df_joined = df1.join(df2,(df1.col1_1==df2.col1_2) & (df1.col2_1==df2.col2_2),'left_outer')
但是,df_joined
中的行数减少了。我做错了什么?
答案 0 :(得分:0)
在documentation中声明:
# for more than one conditions:
cond = [df.name == df3.name, df.age == df3.age]
df.join(df3, cond, 'outer').select(df.name, df3.age).collect()
[Row(name=u'Alice', age=2), Row(name=u'Bob', age=5)]
我目前无法对此进行验证,但以下情况对您有用吗? :
df_joined = df1.join(df2,[df1.col1_1==df2.col1_2, df1.col2_1==df2.col2_2],'left_outer')