2

I'm looking for a way to save a dataframe into a csv file without specifying headers.

I tried the code below but it did not work.

ratingsDF.coalesce(1).write.option("header", "false")\
.csv("csv_file_without_headers.csv")

For the dataframe below:

+---------+-----+
|   x|   y|    z|
+----+----+-----+
|   0|   a|    5|
|   1|   b|   12|
|   2|   c|    7|
|   3|   d|   27|
|   4|   e|  149|
|   5|   f|   19|
+---------+-----+

The expected result of csv:

O,a,5
1,b,12
2,c,7
3,d,27
4,e,149
5,f,19

3 Answers 3

2

Your option looks correct and csv files that is getting written will not be having headers.

In Spark it is not possible to write to file csv_file_without_headers.csv instead check for csv_file_without_headers.csv directory.

  • In the directory you can see all the files in the directory with out header.
Sign up to request clarification or add additional context in comments.

Comments

1

Try this,

ratingsDF.coalesce(1).write.csv("/path/to/save/csv/")

where it will save the csv without header by default. You cannot specify the csv file name but the path only.

1 Comment

Thank you. Even with the code I've put, a directory named csv_file_without_headers.csv was created. And the csv file is inside it
0

You can simply do:

ratingsDF.coalesce(1).write.csv("csv_file_without_headers.csv", header = False)

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.