Oharra
Baimena behar duzu orria atzitzeko. Direktorioetan saioa has dezakezu edo haiek alda ditzakezu.
Baimena behar duzu orria atzitzeko. Direktorioak alda ditzakezu.
Convierte una columna que contiene una StructType en una cadena CSV. Produce una excepción, en el caso de un tipo no admitido.
Syntax
from pyspark.sql import functions as sf
sf.to_csv(col, options=None)
Parámetros
| Parámetro | Tipo | Description |
|---|---|---|
col |
pyspark.sql.Column o str |
Nombre de la columna que contiene una estructura. |
options |
dict, opcional | Opciones para controlar la conversión. Acepta las mismas opciones que el origen de datos CSV. |
Devoluciones
pyspark.sql.Column: una cadena CSV convertida a partir de la clase especificada StructType.
Examples
Ejemplo 1: Convertir un StructType simple en una cadena CSV
from pyspark.sql import Row, functions as sf
data = [(1, Row(age=2, name='Alice'))]
df = spark.createDataFrame(data, ("key", "value"))
df.select(sf.to_csv(df.value)).show()
+-------------+
|to_csv(value)|
+-------------+
| 2,Alice|
+-------------+
Ejemplo 2: Conversión de un StructType complejo en una cadena CSV
from pyspark.sql import Row, functions as sf
data = [(1, Row(age=2, name='Alice', scores=[100, 200, 300]))]
df = spark.createDataFrame(data, ("key", "value"))
df.select(sf.to_csv(df.value)).show(truncate=False)
+-------------------------+
|to_csv(value) |
+-------------------------+
|2,Alice,"[100, 200, 300]"|
+-------------------------+
Ejemplo 3: Conversión de un StructType con valores NULL en una cadena CSV
from pyspark.sql import Row, functions as sf
from pyspark.sql.types import StructType, StructField, IntegerType, StringType
data = [(1, Row(age=None, name='Alice'))]
schema = StructType([
StructField("key", IntegerType(), True),
StructField("value", StructType([
StructField("age", IntegerType(), True),
StructField("name", StringType(), True)
]), True)
])
df = spark.createDataFrame(data, schema)
df.select(sf.to_csv(df.value)).show()
+-------------+
|to_csv(value)|
+-------------+
| ,Alice|
+-------------+
Ejemplo 4: Conversión de un StructType con distintos tipos de datos en una cadena CSV
from pyspark.sql import Row, functions as sf
data = [(1, Row(age=2, name='Alice', isStudent=True))]
df = spark.createDataFrame(data, ("key", "value"))
df.select(sf.to_csv(df.value)).show()
+-------------+
|to_csv(value)|
+-------------+
| 2,Alice,true|
+-------------+