python-3.x - 处理 pyspark 数据框中的空值

标签 python-3.x dataframe pyspark azure-databricks

我有 pyspark dataframe 和一些数据,我想要substring 列的一些数据,该列还包含一些 null 值。 这是我的数据框

+-------------+
|          Name|
+--------------+
| Asia201909284|
|    US20190928|
|Europ201909287|
|          null|
|     something|
|       nothing|
+--------------+

我想从名称列中删除亚洲、美国、欧洲

这是我已经尝试过的代码。

fun_asia = udf(lambda x: x[4:len(x)])
fun_us = udf(lambda x: x[2:len(x)])
fun_europ = udf(lambda x: x[5:len(x)])
df1.withColumn("replace", \
               when(df1.Name.isNull(),df1.Name)\
               .when(df1.Name.like("Asia%"),fun_asia(col('Name')))\
               .when(df1.Name.like("US%"),fun_us(col('Name')))\
               .when(df1.Name.like("Europ%"),fun_europ(col('Name')))
               .otherwise(df1.Name)
              ).show()

如果该列中没有 null 值,则它可以正常工作。但如果有一些 null 值,则会出现类似 len() cantcalculate null value 的错误。

错误消息

TypeError:“NoneType”类型的对象没有 len()

我很困惑为什么它的调用 fun 也适用于 null 值。 我怎样才能克服我的问题并得到我想要的结果,任何帮助表示赞赏。

我想要的实际结果

+--------------+---------+
|          Name|  replace|
+--------------+---------+
| Asia201909284|201909284|
|    US20190928| 20190928|
|Europ201909287|201909287|
|          null|     null|
|     something|something|
|       nothing|  nothing|
+--------------+---------+

最佳答案

一种方法是使用 whenisNull() 条件来处理 when 列为 null 条件:

df1.withColumn("replace", \
               when(df1.Name.like("Asia%"),fun_asia(col('Name')))\
               .when(df1.Name.like("US%"),fun_us(col('Name')))\
               .when(df1.Name.like("Europ%"),fun_europ(col('Name')))
               .when(df1.Name.isNull(), df1.Name)
               .otherwise(df1.Name)
              ).show()

编辑2:

您可以更改 udf 来处理空值:

fun_asia = udf(lambda x: x[4:len(x)] if x else None)
fun_us = udf(lambda x: x[2:len(x)] if x else None)
fun_europ = udf(lambda x: x[5:len(x)] if x else None)
df1.withColumn("replace", \
               when(df1.Name.isNull(),df1.Name)\
               .when(df1.Name.like("Asia%"),fun_asia(col('Name')))\
               .when(df1.Name.like("US%"),fun_us(col('Name')))\
               .when(df1.Name.like("Europ%"),fun_europ(col('Name')))
               .otherwise(df1.Name)
              ).show()
+--------------+---------+
|          Name|  replace|
+--------------+---------+
| Asia201909284|201909284|
|    US20190928| 20190928|
|Europ201909287|201909287|
|          null|     null|
|     something|something|
|       nothing|  nothing|
+--------------+---------+

关于python-3.x - 处理 pyspark 数据框中的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58424076/

相关文章:

python - Pyspark:从日期时间值中提取日期

apache-spark - 将两个数据框中的行与最近点连接起来

python-3.x - 在 python 中使用枚举和简单的类变量调用有什么区别?

python - 如何在 python 文件中包含文本

Python:将 Pandas Dataframe 写入 MSSQL --> 数据库错误

python - 根据另一列减去某个 pandas 数据框列的最小值

python - 我如何使用 spark-env.sh.template 在 Python 3 中设置 Pyspark

python-3.x - "time.sleep()"在使用 "end"属性的带有打印函数的 for 循环中不起作用吗?

python - 如果函数花费时间超过 5 秒,则传递该函数

python - 在不迭代的情况下向量化 Pandas DataFrame 上的乘法和字典映射?