【问题标题】:check if a row value is null in spark dataframe检查火花数据框中的行值是否为空
【发布时间】:2016-12-26 10:27:13
【问题描述】:

我在 pyspark 中使用自定义函数来检查 spark 数据框中每一行的条件,如果条件为真,则添加列。

代码如下:

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'

如何在我的自定义函数中检查当前行中特定列的空值?

【问题讨论】:

  • 你能show你的Dataframe或者至少打印它的架构吗?
  • Dataframe 的架构是:root |-- id: string (nullable = true) |-- code: string (nullable = true) |-- prod_code: string (nullable = true) |--产品:字符串(可为空 = true)

标签: apache-spark pyspark user-defined-functions spark-dataframe isnull


【解决方案1】:

考虑到sdfDataFrame,您可以使用select 语句。

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

【讨论】:

  • 但我需要对数据框的不同列进行多次操作,因此想使用自定义函数。为什么我可以在自定义函数中检查空值?
  • 您需要修改问题,并添加您的要求。
猜你喜欢
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多