Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if a row value is null in spark dataframe

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?

like image 632
sam Avatar asked Aug 19 '16 09:08

sam


1 Answers

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

sdf.select("*", when(col("pro").isNull(), lit("new pro")).otherwise(col("pro")))
like image 67
Alberto Bonsanto Avatar answered Nov 18 '22 22:11

Alberto Bonsanto