简体   繁体   中英

How to save a DataFrame as a csv-file using pyspark?

Why does this approach not work?

from pyspark.sql import SparkSession

spark = SparkSession.builder.appName('session').getOrCreate()
df = spark.range(5).toDF("index")
filepath = r"C:/my_favorite_directory"
df.write.csv(filepath)

Update

The above code works fine, the problem was that I had not specified the hadoop binary path to refer to the winutils binary, which is needed by pyspark to write csv-files.

Your filepath should ends with .csv or file extension. You are providing directory thats wrong

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM