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