检查spark数据框中的行值是否为null [英] check if a row value is null in spark dataframe
本文介绍了检查spark数据框中的行值是否为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在pyspark中使用自定义函数来检查spark数据框中每一行的条件,并在条件为true时添加列.
I am using a custom function in pyspark to check a condition for each row in a spark dataframe and add columns if condition is true.
代码如下:
from pyspark.sql.types import *
from pyspark.sql.functions import *
from pyspark.sql import Row
def customFunction(row):
if (row.prod.isNull()):
prod_1 = "new prod"
return (row + Row(prod_1))
else:
prod_1 = row.prod
return (row + Row(prod_1))
sdf = sdf_temp.map(customFunction)
sdf.show()
我收到以下错误提示:
AttributeError:"unicode"对象没有属性"isNull"
如何在自定义函数中检查当前行中特定列的空值?
How can I check for null values for specific columns in the current row in my custom function?
推荐答案
考虑到sdf
是DataFrame
,您可以使用select
语句.
Considering that sdf
is a DataFrame
you can use a select
statement.
sdf.select("*", when(col("pro").isNull(), lit("new pro")).otherwise(col("pro")))
这篇关于检查spark数据框中的行值是否为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文