リストagg_distinct
集計関数: 区切り文字で区切られた、異なる null 以外の入力値の連結を返します。
構文
Python
import pyspark.sql.functions as sf
sf.listagg_distinct(col=<col>)
# With delimiter
sf.listagg_distinct(col=<col>, delimiter=<delimiter>)
パラメーター
パラメーター | Type | 説明 |
|---|---|---|
|
| ターゲットカラムをコンピュートに。 |
|
| オプション。値を区切る区切り文字。デフォルト値は「なし」です。 |
戻り値
pyspark.sql.Column: コンピュート結果の列。
例
例 1 : listagg_distinct 関数を使用する。
Python
import pyspark.sql.functions as sf
df = spark.createDataFrame([('a',), ('b',), (None,), ('c',), ('b',)], ['strings'])
df.select(sf.listagg_distinct('strings')).show()
Output
+-------------------------------+
|listagg(DISTINCT strings, NULL)|
+-------------------------------+
| abc|
+-------------------------------+
例 2 : 区切り文字を使用した listagg_distinct 関数の使用。
Python
import pyspark.sql.functions as sf
df = spark.createDataFrame([('a',), ('b',), (None,), ('c',), ('b',)], ['strings'])
df.select(sf.listagg_distinct('strings', ', ')).show()
Output
+-----------------------------+
|listagg(DISTINCT strings, , )|
+-----------------------------+
| a, b, c|
+-----------------------------+
例 3 : バイナリ列と区切り文字を使用した listagg_distinct 関数の使用。
Python
import pyspark.sql.functions as sf
df = spark.createDataFrame([(b'\x01',), (b'\x02',), (None,), (b'\x03',), (b'\x02',)],
['bytes'])
df.select(sf.listagg_distinct('bytes', b'\x42')).show()
Output
+------------------------------+
|listagg(DISTINCT bytes, X'42')|
+------------------------------+
| [01 42 02 42 03]|
+------------------------------+
例 4 : すべての None 値を持つ列で listagg_distinct 関数を使用する。
Python
import pyspark.sql.functions as sf
from pyspark.sql.types import StructType, StructField, StringType
schema = StructType([StructField("strings", StringType(), True)])
df = spark.createDataFrame([(None,), (None,), (None,), (None,)], schema=schema)
df.select(sf.listagg_distinct('strings')).show()
Output
+-------------------------------+
|listagg(DISTINCT strings, NULL)|
+-------------------------------+
| NULL|
+-------------------------------+