0
假设您尝试从数据框的列中提取子字符串。 regexp_extract()
如果字段本身为空则返回null,但如果字段不为null但未找到该表达式,则返回空字符串。如何为后一种情况返回空值?Spark:从失败的regexp_extract()返回null
df = spark.createDataFrame([(None),('foo'),('foo_bar')], StringType())
df.select(regexp_extract('value', r'_(.+)', 1).alias('extracted')).show()
# +---------+
# |extracted|
# +---------+
# | null|
# | |
# | bar|
# +---------+
我一直在使用基于zero323的回答[这里](类似的方法http://stackoverflow.com/questions/33287886/更换空弦与 - 没有无效值 - 在非数据帧)。像上面所做的那样,最好使用udf吗? – evilpilotfish