Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.
I have created a notebook and a spark data frame, when I write the data frame into a csv file, the column header is not written into csv. Is that normal?
***
yearlySales = df.select(year(col("OrderDate")).alias("Year")).groupBy("Year").count().orderBy("Year")
yearlySales.write.mode("overwrite").format("csv").save("Files/yearlySales.csv")
***
Solved! Go to Solution.
Hi @Jeanxyz
yearlySales.write.mode("overwrite").option("header", True).format("csv").save("Files/yearlySales.csv")
option(“header”, True): Ensures that the column headers are written as the first row in the CSV output.
Hi @Jeanxyz,
Thank you for reaching out to the Microsoft Fabric Forum Community.
Check the solution provided by @nilendraFabric, adding.option("header", True) to your .write() operation will ensure the column headers are written as the first row in your CSV file.
You can also refer to the official Apache Spark documentation for more context on the header option:
CSV Files - Spark 3.3.2 Documentation
If this post helps, then please give us ‘Kudos’ and consider Accept it as a solution to help the other members find it more quickly.
Thank you.
Thanks a lot, @nilendraFabric . This works. I have yet another question. What's the best Spark tutorial online? I searched on https://spark.apache.org/, but the documentation about dataframe write method seems quite limited.
Hi @Jeanxyz
yearlySales.write.mode("overwrite").option("header", True).format("csv").save("Files/yearlySales.csv")
option(“header”, True): Ensures that the column headers are written as the first row in the CSV output.