且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spark:将具有空值的CSV写入为空列

更新时间:2022-04-02 08:35:57

轻松设置了emptyValue选项

emptyValue:设置一个空值的字符串表示形式.如果设置为None,则使用默认值"".

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='')

输出

col_1,col_2,col_3
,20151231,Hello
2,20160101,
3,,World