【发布时间】:2020-10-05 10:05:17
【问题描述】:
我有如下数据:
from pyspark.sql import SparkSession, Row
import pyspark.sql.functions as F
dd = spark.createDataFrame([
('0', [Row(f1=0),Row(f1=1),Row(f1=None)]),
('1', [Row(f1=None), Row(f1=2)]),
('2', [])
], ['id', 'arr'])
并且想要一个包含“arr”数组中第一个非零元素的新列,或者为空。在这种情况下:
id | target_elt
0 | 1
1 | 2
2 | Null
请注意,数组元素的类型为 Struct,其 IntegerType 字段为“f1”
我的尝试:
positiveNonNull = F.udf(
lambda array: [
x.f1 for x in array
if (x.f1 is not None) & (x.f1 > 0)
], ArrayType(LongType())
)
dd.withColumn('newcol', positiveNonNull(F.col('arr')).getItem(0)).show()
我得到 TypeError: '>=' not supported between 'NoneType' and 'int'
【问题讨论】: