DataFrameWriter.
csv
Saves the content of the DataFrame in CSV format at the specified path.
DataFrame
New in version 2.0.0.
Changed in version 3.4.0: Supports Spark Connect.
the path in any Hadoop supported file system
specifies the behavior of the save operation when data already exists.
append: Append contents of this DataFrame to existing data.
append
overwrite: Overwrite existing data.
overwrite
ignore: Silently ignore this operation if data already exists.
ignore
error
errorifexists
exists.
For the extra options, refer to Data Source Option for the version you use.
Examples
Write a DataFrame into a CSV file and read it back.
>>> import tempfile >>> with tempfile.TemporaryDirectory() as d: ... # Write a DataFrame into a CSV file ... df = spark.createDataFrame([{"age": 100, "name": "Hyukjin Kwon"}]) ... df.write.csv(d, mode="overwrite") ... ... # Read the CSV file as a DataFrame with 'nullValue' option set to 'Hyukjin Kwon'. ... spark.read.schema(df.schema).format("csv").option( ... "nullValue", "Hyukjin Kwon").load(d).show() +---+----+ |age|name| +---+----+ |100|null| +---+----+