You can also provide a list of strings, if the column names are the same.
df1 = sqlContext.createDataFrame(
[(1, "a", 2.0), (2, "b", 3.0), (3, "c", 3.0)],
("x1", "x2", "x3"))
df2 = sqlContext.createDataFrame(
[(1, "f", -1.0), (2, "b", 0.0)], ("x1", "x2", "x3"))
df = df1.join(df2, ["x1","x2"])
df.show()
+---+---+---+---+
| x1| x2| x3| x3|
+---+---+---+---+
| 2| b|3.0|0.0|
+---+---+---+---+
Another way to go about this, if column names are different and if you want to rely on column name strings is the following:
df1 = sqlContext.createDataFrame(
[(1, "a", 2.0), (2, "b", 3.0), (3, "c", 3.0)],
("x1", "x2", "x3"))
df2 = sqlContext.createDataFrame(
[(1, "f", -1.0), (2, "b", 0.0)], ("y1", "y2", "y3"))
df = df1.join(df2, (col("x1")==col("y1")) & (col("x2")==col("y2")))
df.show()
+---+---+---+---+---+---+
| x1| x2| x3| y1| y2| y3|
+---+---+---+---+---+---+
| 2| b|3.0| 2| b|0.0|
+---+---+---+---+---+---+
This is useful if you want to reference column names dynamically and also in instances where there is a space in the column name and you cannot use the df.col_name
syntax. You should look at changing the column name in that case anyway though.