PySpark SQL 函數的 array(~) 方法將多個列組合成一列數組。
注意
如果要組合 array-type 的多個列,請改用  concat(~) 。
參數
1.*cols | string 或 Column
要組合的列。
返回值
新的 PySpark 列。
例子
考慮以下PySpark DataFrame:
df = spark.createDataFrame([['A', 'a', '1'], ['B', 'b', '2'], ['C', 'c', '3']], ['col1', 'col2', 'col3'])
df.show()
+----+----+----+
|col1|col2|col3|
+----+----+----+
|   A|   a|   1|
|   B|   b|   2|
|   C|   c|   3|
+----+----+----+要將 col1 、 col2 和 col3 列合並到單列數組中,請使用 array(~) 方法:
from pyspark.sql import functions as F
# Assign label to PySpark column returned by array(~) using alias(~)
df.select(F.array('col1','col2','col3').alias('combined_col')).show()
+------------+
|combined_col|
+------------+
|   [A, a, 1]|
|   [B, b, 2]|
|   [C, c, 3]|
+------------+我們還可以提供 Column 對象,而不是傳遞列標簽:
df.select(F.array(F.col('col1'),df['col2'],'col3').alias('combined_col')).show()
+------------+
|combined_col|
+------------+
|   [A, a, 1]|
|   [B, b, 2]|
|   [C, c, 3]|
+------------+相關用法
- Python PySpark SQL Functions split方法用法及代碼示例
- Python PySpark SQL Functions repeat方法用法及代碼示例
- Python PySpark SQL Functions explode方法用法及代碼示例
- Python PySpark SQL Functions concat方法用法及代碼示例
- Python PySpark SQL Functions instr方法用法及代碼示例
- Python PySpark SQL Functions count_distinct方法用法及代碼示例
- Python PySpark SQL Functions dayofmonth方法用法及代碼示例
- Python PySpark SQL Functions date_add方法用法及代碼示例
- Python PySpark SQL Functions concat_ws方法用法及代碼示例
- Python PySpark SQL Functions col方法用法及代碼示例
- Python PySpark SQL Functions translate方法用法及代碼示例
- Python PySpark SQL Functions dayofweek方法用法及代碼示例
- Python PySpark SQL Functions expr方法用法及代碼示例
- Python PySpark SQL Functions regexp_extract方法用法及代碼示例
- Python PySpark SQL Functions regexp_replace方法用法及代碼示例
- Python PySpark SQL Functions round方法用法及代碼示例
- Python PySpark SQL Functions countDistinct方法用法及代碼示例
- Python PySpark SQL Functions date_format方法用法及代碼示例
- Python PySpark SQL Functions collect_list方法用法及代碼示例
- Python PySpark SQL Functions lit方法用法及代碼示例
- Python PySpark SQL Functions upper方法用法及代碼示例
- Python PySpark SQL Functions length方法用法及代碼示例
- Python PySpark SQL Functions dayofyear方法用法及代碼示例
- Python PySpark SQL Functions trim方法用法及代碼示例
- Python PySpark SQL Functions month方法用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 PySpark SQL Functions | array method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
