0

I am working in Pyspark environment in Databricks and have a pyspark data frame which I will call as df.

I need to push this spark dataframe into csv file, I am unable to do so. Though there is no error popping up but the dataframe doesn’t get copied into the csv. Below is the generic code

path = “ “ #CSV File Location
header = “This is the header of the file"
With open(path,”a”) as f:
    f.write(header+”\n”)
    df.write.csv(path=path,format=“csv”,mode=“append”)
    f.close

Only the header gets reflected in the file and not the dataframe

1 Answer 1

1

You can write your dataframe as csv using this:

df.coalesce(1).write.format("com.databricks.spark.csv").option("header", "true").save("dbfs:/FileStore/df.csv")

Coalesce avoids saving it in multiple partitions. You can put in your own path as parameter in save().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.