1

我在pyspark中使用自定義函數檢查火花數據框中每行的條件,並在條件爲真時添加列。檢查火花數據框中的行值是否爲空

的代碼如下:

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的: '統一' 對象有沒有屬性 '的isNull'

我如何檢查爲我自定義函數中當前行中特定列的空值?

+1

你可以'顯示''Dataframe'或至少打印它的模式嗎? –

+0

Dataframe的架構是:根 | - id:string(nullable = true) | - code:string(nullable = true) | - prod_code:string(nullable = true) | - prod:string (nullable = true) – sam

回答

4

考慮到sdfDataFrame您可以使用select語句。

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

但是我需要在數據框的不同列上做幾個操作,因此想要使用自定義函數。爲什麼我可以檢查自定義函數中的空值? – sam

+1

您需要修改問題,並添加您的要求。 –