pyspark.sql.DataFrameWriter.options

DataFrameWriter.options(**options: OptionalPrimitiveType) → DataFrameWriter[source]

Adds output options for the underlying data source.

New in version 1.4.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
**optionsdict

The dictionary of string keys and primitive-type values.

Examples

>>> spark.range(1).write.option("key", "value")
<...readwriter.DataFrameWriter object ...>

Specify the option ‘nullValue’ and ‘header’ with writing a CSV file.

>>> from pyspark.sql.types import StructType,StructField, StringType, IntegerType
>>> schema = StructType([
...     StructField("age",IntegerType(),True),
...     StructField("name",StringType(),True),
... ])
>>> import tempfile
>>> with tempfile.TemporaryDirectory() as d:
...     # Write a DataFrame into a CSV file with 'nullValue' option set to 'Hyukjin Kwon',
...     # and 'header' option set to `True`.
...     df = spark.createDataFrame([(100, None)], schema=schema)
...     df.write.options(nullValue="Hyukjin Kwon", header=True).mode(
...         "overwrite").format("csv").save(d)
...
...     # Read the CSV file as a DataFrame.
...     spark.read.option("header", True).format('csv').load(d).show()
+---+------------+
|age|        name|
+---+------------+
|100|Hyukjin Kwon|
+---+------------+