check if a row value is null in spark dataframe

泄露秘密 提交于 2020-05-08 05:36:17

问题


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.

The code is as below:

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()

I get the error mention below:

AttributeError: 'unicode' object has no attribute 'isNull'

How can I check for null values for specific columns in the current row in my custom function?


回答1:


Considering that sdf is a DataFrame you can use a select statement.

sdf.select("*", when(col("pro").isNull(), lit("new pro")).otherwise(col("pro")))


来源:https://stackoverflow.com/questions/39036005/check-if-a-row-value-is-null-in-spark-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!