listagg
Função agregada: retorna a concatenação de valores de entrada não nulos, separados pelo delimitador.
Para a função Databricks SQL correspondente, consulte listagg função agregada.
Sintaxe
Python
import pyspark.sql.functions as sf
sf.listagg(col=<col>)
# With delimiter
sf.listagg(col=<col>, delimiter=<delimiter>)
Parâmetros
Parâmetro | Tipo | Descrição |
|---|---|---|
|
| coluna de destino para compute . |
|
| Opcional. O delimitador para separar os valores. O valor default é Nenhum. |
Devoluções
pyspark.sql.Column: a coluna para resultados de cálculo.
Exemplos
Exemplo 1 : Usando a função listagg.
Python
import pyspark.sql.functions as sf
df = spark.createDataFrame([('a',), ('b',), (None,), ('c',)], ['strings'])
df.select(sf.listagg('strings')).show()
Output
+----------------------+
|listagg(strings, NULL)|
+----------------------+
| abc|
+----------------------+
Exemplo 2 : Usando a função listagg com um delimitador.
Python
import pyspark.sql.functions as sf
df = spark.createDataFrame([('a',), ('b',), (None,), ('c',)], ['strings'])
df.select(sf.listagg('strings', ', ')).show()
Output
+--------------------+
|listagg(strings, , )|
+--------------------+
| a, b, c|
+--------------------+
Exemplo 3 : Usando a função listagg com uma coluna binária e um delimitador.
Python
import pyspark.sql.functions as sf
df = spark.createDataFrame([(b'\x01',), (b'\x02',), (None,), (b'\x03',)], ['bytes'])
df.select(sf.listagg('bytes', b'\x42')).show()
Output
+---------------------+
|listagg(bytes, X'42')|
+---------------------+
| [01 42 02 42 03]|
+---------------------+
Exemplo 4 : Usando a função listagg em uma coluna com todos os valores None.
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('strings')).show()
Output
+----------------------+
|listagg(strings, NULL)|
+----------------------+
| NULL|
+----------------------+