【问题标题】:Handling null value in pyspark dataframe处理 pyspark 数据框中的空值
【发布时间】:2020-02-13 20:53:15
【问题描述】:

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

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

我想从Name 列中删除Asia, US, Europ

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

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() 无法计算空值的错误。

错误消息

TypeError: object of type 'NoneType' has no len()

我很困惑为什么它的调用乐趣也为null 值。 以及如何克服我的问题并获得我想要的结果,任何帮助表示感谢。

我想要的实际结果

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

【问题讨论】:

  • 为什么不删除空值?
  • 它的演示数据框这就是为什么我只显示一列,但在我的真实数据框中有不止一列,所以我需要那个也有空值的记录。
  • 我想我在下面提供了正确的解决方案。请尝试一下。如果有帮助,请点赞并选择。尝试第二种解决方案,我认为对您来说更好。

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


【解决方案1】:

一种方法是使用带有isNull() 条件的when 来处理when column is 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|
+--------------+---------+

【讨论】:

  • 感谢您的回复,首先我需要空值的那一行,所以我不能删除,我的问题是如何处理 null 值而不删除或删除。我也尝试使用isNull() 选项(你的答案的第二部分),但结果是一样的。对不起,我忘了提。
  • 供您参考,第一个解决方案有效,但它删除了null 行,这不是我的解决方案,第二个不正确,
  • @SohelReza 请查看更新演示。我认为它应该可以达到您的预期效果
  • 很抱歉,你没有明白我的意思,请仔细看我的演示数据,然后你会意识到我不能像你在上一个解决方案中那样简单地替换我的整个数据,而且我不知道在我想消除的那部分之后会有多少字符。请再仔细阅读我的问题。
  • @SohelReza 第二个解决方案不会删除任何行,也不会替换任何内容。它只替换您要替换的内容,如果有null,则返回null,否则返回df.Name。请仔细阅读解决方案。该演示是一个示例,而不是您的解决方案
猜你喜欢
  • 2020-04-28
  • 2020-11-29
  • 2020-01-04
  • 2022-06-14
  • 2017-06-25
  • 1970-01-01
  • 2023-03-17
  • 2017-11-26
  • 2020-04-27
相关资源
最近更新 更多