回傳一個新的資料框架,包含該資料框架與另一個資料框架中列的聯集。
語法
unionByName(other: "DataFrame", allowMissingColumns: bool = False)
參數
| 參數 | 類型 | 說明 |
|---|---|---|
other |
資料框架 | 另一個需要合併的資料框架。 |
allowMissingColumns |
bool、可選、預設 False | 請指定是否允許缺失欄位。 |
退貨
DataFrame:一個新的資料框架,包含兩個給定資料框架的合併列及其對應欄位。
Notes
此方法對兩個輸入資料框執行聯合操作,並以名稱(而非位置)解析欄位。 當 為真時 allowMissingColumns ,缺失欄位將被填入 null。
Examples
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|
# +----+----+----+----+