我使用 Spark 2.1 和 python 2.7.12。
假设如下:
from pyspark.sql.functions import *
import timestamp
data = [Row(time=datetime.datetime(2017, 1, 1, 0, 0, 0, 0)), Row (time=datetime.datetime(1980, 1, 1, 0, 0, 0, 0)), Row(time=None) ]
df = spark.createDataFrame(data)
使用方法
df.fillna({'time': datetime.datetime(1980, 1, 1, 0, 0, 0, 0)})
填写 null
特定时间的值/秒?
最佳答案
你可以试试 coalesce
:
from pyspark.sql.functions import *
default_time = datetime.datetime(1980, 1, 1, 0, 0, 0, 0)
result = df.withColumn('time', coalesce(col('time'), lit(default_time)))
或者,如果您想继续使用
fillna
,您需要以标准格式将默认值作为字符串传递:from pyspark.sql.functions import *
default_time = '1980-01-01 00:00:00'
result = df.fillna({'time': default_time})
关于apache-spark - 如何在DataFrame中使用具体时间戳填充无值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43995736/