Compartir a través de


string_agg_distinct

Función de agregado: devuelve la concatenación de valores de entrada distintos que no son NULL, separados por el delimitador. Alias de listagg_distinct.

Syntax

import pyspark.sql.functions as sf

sf.string_agg_distinct(col=<col>)

# With delimiter
sf.string_agg_distinct(col=<col>, delimiter=<delimiter>)

Parámetros

Parámetro Tipo Description
col pyspark.sql.Column o str Columna de destino en la que se va a calcular.
delimiter pyspark.sql.Column, str o bytes Optional. Delimitador para separar los valores. El valor predeterminado es Ninguno.

Devoluciones

pyspark.sql.Column: la columna para los resultados calculados.

Examples

Ejemplo 1: Uso de string_agg_distinct función.

import pyspark.sql.functions as sf
df = spark.createDataFrame([('a',), ('b',), (None,), ('c',), ('b',)], ['strings'])
df.select(sf.string_agg_distinct('strings')).show()
+----------------------------------+
|string_agg(DISTINCT strings, NULL)|
+----------------------------------+
|                               abc|
+----------------------------------+

Ejemplo 2: Uso de string_agg_distinct función con un delimitador.

import pyspark.sql.functions as sf
df = spark.createDataFrame([('a',), ('b',), (None,), ('c',), ('b',)], ['strings'])
df.select(sf.string_agg_distinct('strings', ', ')).show()
+--------------------------------+
|string_agg(DISTINCT strings, , )|
+--------------------------------+
|                         a, b, c|
+--------------------------------+

Ejemplo 3: Uso de string_agg_distinct función con una columna binaria y un delimitador.

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()
+---------------------------------+
|string_agg(DISTINCT bytes, X'42')|
+---------------------------------+
|                 [01 42 02 42 03]|
+---------------------------------+

Ejemplo 4: Uso de string_agg_distinct función en una columna con todos los valores None.

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()
+----------------------------------+
|string_agg(DISTINCT strings, NULL)|
+----------------------------------+
|                              NULL|
+----------------------------------+