pyspark.sql.functions.to_csv

pyspark.sql.functions.to_csv(col, options={})[source]

Converts a column containing a StructType into a CSV string. Throws an exception, in the case of an unsupported type.

Parameters
  • col – name of column containing a struct.

  • options – options to control converting. accepts the same options as the CSV datasource.

>>> from pyspark.sql import Row
>>> data = [(1, Row(name='Alice', age=2))]
>>> df = spark.createDataFrame(data, ("key", "value"))
>>> df.select(to_csv(df.value).alias("csv")).collect()
[Row(csv='2,Alice')]

New in version 3.0.