【发布时间】:2021-02-28 10:18:07
【问题描述】:
我正在使用以下代码示例:
paths = ["/FileStore/tables/data.csv"]
infer_schema = "true"
df= sqlContext.read \
.format("com.databricks.spark.csv") \
.option("inferSchema", infer_schema) \
.option("header", "true") \
.load(paths)
df.printSchema()
root |-- 键:字符串(可为空=真)|-- dt: string (nullable = true) |-- key1: string (nullable = true) |-- key2: string (nullable = true) |-- sls: string (nullable = true) |-- uts: 字符串 (nullable = true) |-- key3: 字符串(可为空=真)
我执行以下操作来计算字段 sls 和 uts 的空值
df.select([count(when(col(c).isNull(), c)).alias(c) for c in df.columns]).show()
+-------------+--------+------------------+-----------+-----+-----+---------+
|key| dt| key1| key2| sls| uts| key3|
+-------------+--------+------------------+-----------+-----+-----+---------+
| 0| 0| 0| 0| 616| 593| 0|
+-------------+--------+------------------+-----------+-----+-----+---------+
我首先做了以下事情:
df.na.fill({'sls': 0, 'uts': 0})
然后我意识到这些是字符串字段。所以,我做到了:
df.na.fill({'sls': '0', 'uts': '0'})
这样做之后,如果我这样做:
df.filter("sls is NULL").show()
我看到 null 字段的 sls 值:
key| dt| key1| key2| sls| uts| key3|
+-------------+----------+------------------+-----------+-----+-----+-----------+
| -1| 7/13/2020| 8000|41342299215| null| 1|1.70228E+25|
| -1| 12/5/2019| 8734| 8983349833| null| 1|1.76412E+26|
| -1| 1/7/2020| 8822| 1E+15| null| 1|4.69408E+24|
| -1| 12/5/2018| 6768| 1E+15| null| 1|4.54778E+24|
如果我这样做也是一样的:
df.filter("uts is NULL").show()
我有什么遗漏吗?为什么我无法用0 替换空值?
【问题讨论】: