Code python

PySpark DataFrame - inner, left and right Joins

Kontext Kontext visibility 101 comment 0 access_time 2 years ago language English

descriptionCode description

PySpark DataFrame - inner, left and right Joins

This code snippet shows you how to perform inner, left and right joins with DataFrame.join API. 

def join(self, other, on=None, how=None)

Supported join types

The default join type is inner. The supported values for parameter how are: inner, cross, outer, full, fullouter, full_outer, left, leftouter, left_outer, right, rightouter, right_outer, semi, leftsemi, left_semi, anti, leftanti and left_anti.

To learn about the these different join types, refer to article Spark SQL Joins with Examples.

Join via multiple columns

If there are more than one column to join, we can specify on parameter as a list of column name:

df1.join(df2, on=['id','other_column'], how='left')

Output from the code snippet:

+---+----+
| id|attr|
+---+----+
|  1|   A|
|  2|   B|
+---+----+

+---+--------+
| id|attr_int|
+---+--------+
|  1|     100|
|  2|     200|
|  3|     300|
+---+--------+

+---+----+--------+
| id|attr|attr_int|
+---+----+--------+
|  1|   A|     100|
|  2|   B|     200|
+---+----+--------+

+---+----+--------+
| id|attr|attr_int|
+---+----+--------+
|  1|   A|     100|
|  2|   B|     200|
+---+----+--------+

+---+----+--------+
| id|attr|attr_int|
+---+----+--------+
|  1|   A|     100|
|  2|   B|     200|
|  3|null|     300|
+---+----+--------+
fork_rightFork
more_vert
copyright This page is subject to Site terms.
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts