unionByName

返回一个新的数据帧,其中包含此数据帧和另一个 DataFrame 中的行的联合。

Syntax

unionByName(other: "DataFrame", allowMissingColumns: bool = False)

参数

参数 类型 说明
other DataFrame 需要组合的另一个数据帧。
allowMissingColumns bool、optional、default False 指定是否允许缺少的列。

退货

DataFrame:包含组合行的新数据帧,其中包含两个给定数据帧的对应列。

备注

此方法对两个输入数据帧执行联合操作,按名称(而不是位置)解析列。 如果 allowMissingColumns 为 True,则缺少的列将填充 null。

示例

df1 = spark.createDataFrame([[1, 2, 3]], ["col0", "col1", "col2"])
df2 = spark.createDataFrame([[4, 5, 6]], ["col1", "col2", "col0"])
df1.unionByName(df2).show()
# +----+----+----+
# |col0|col1|col2|
# +----+----+----+
# |   1|   2|   3|
# |   6|   4|   5|
# +----+----+----+

df1 = spark.createDataFrame([[1, 2, 3]], ["col0", "col1", "col2"])
df2 = spark.createDataFrame([[4, 5, 6]], ["col1", "col2", "col3"])
df1.unionByName(df2, allowMissingColumns=True).show()
# +----+----+----+----+
# |col0|col1|col2|col3|
# +----+----+----+----+
# |   1|   2|   3|NULL|
# |NULL|   4|   5|   6|
# +----+----+----+----+