12

I'm using PySpark to write a dataframe to a CSV file like this:

df.write.csv(PATH, nullValue='')

There is a column in that dataframe of type string. Some of the values are null. These null values display like this:

...,"",...

I would like them to be display like this instead:

...,,...

Is this possible with an option in csv.write()?

Thanks!

6
  • Is there a need to set nullValue? If you remove the option, does it default to write nothing at all? Commented Aug 30, 2019 at 12:28
  • It does the same thing with and without nullValue. Setting nullValue='' was my first attempt to fix the problem, which didn't work. Commented Aug 30, 2019 at 12:29
  • You can try to do df.fillna('').write.csv(PATH) instead. Basically force all the null columns to be an empty string. Commented Aug 30, 2019 at 12:31
  • I'm not sure this will work, empty strings are also written as "" in the output CSV. Commented Aug 30, 2019 at 12:36
  • Interesting. If your dataframe is not too large, you might want to consider converting it to a Pandas dataframe then do the write. That might work. Commented Aug 30, 2019 at 12:37

1 Answer 1

20

Easily with emptyValue option setted

emptyValue: sets the string representation of an empty value. If None is set, it use the default value, "".

from pyspark import Row
from pyspark.shell import spark

df = spark.createDataFrame([
    Row(col_1=None, col_2='20151231', col_3='Hello'),
    Row(col_1=2, col_2='20160101', col_3=None),
    Row(col_1=3, col_2=None, col_3='World')
])

df.write.csv(PATH, header=True, emptyValue='')

Output

col_1,col_2,col_3
,20151231,Hello
2,20160101,
3,,World
Sign up to request clarification or add additional context in comments.

2 Comments

It's strange. When I use df.write.format('csv').option('nullValue', None).save(PATH), it writes empty value. But when I use it like df.write.csv(PATH, nullValue=None), it writes emtpy string value - double quotes
I see the same behavior you indicate with setting nullValue, but setting emptyValue as the answer suggests seems to produce the desired output.

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.