AttributeError: 'NoneType' object has no attribute 'upper'

AttributeError: 'NoneType' object has no attribute 'upper'

我有一个 PySpark Dataframe (df) 并试图添加一个列 (capital_names),该列将是一个现有列(名称),名称已转换为大写字母。我是这样做的:

def capital(text):
    return text.upper()

udf_capital = udf(capital,StringType())

df2 = df.withColumn("capital_names",udf_capital("names"))

df2.show()

当 运行 代码时,我在尝试显示生成的数据帧时遇到错误:

AttributeError: 'NoneType' object has no attribute 'upper'

但是,我已经检查了我的数据框的模式并且“名称”列是 StringType,我该如何解决这个问题? 提前致谢!

列中有空值。尝试在 UDF 中添加检查以捕获它。

def capital(text):
    if text is not None:
        return text.upper()
    else:
        return None