我有一个pyspark数据框df
id alias
1 ["jon", "doe"]
2 null
我正在尝试替换空值并使用一个空列表
id alias
1 ["jon", "doe"]
2 []
我尝试使用
.fillna('alias', '[]')
.fillna('alias', create_list([])
和来自Convert null values to empty array in Spark DataFrame
的答案但它们在语法上都不正确。
答案 0 :(得分:0)
由于列类型不同,您不能直接使用fillna。您可以使用以下内容
df.show()
+---+----------+
| id| alias|
+---+----------+
| 1|[jon, doe]|
| 2| null|
+---+----------+
import pyspark.sql.functions as F
df.select([ F.coalesce(F.col(col[0]), F.array()).alias(col[0]) if col[1].startswith('array') else F.col(col[0]) for col in df.dtypes]).show()
+---+----------+
| id| alias|
+---+----------+
| 1|[jon, doe]|
| 2| []|
+---+----------+