【问题标题】:pyspark replace multiple values with null in dataframepyspark在数据框中用null替换多个值
【发布时间】:2019-05-21 22:39:32
【问题描述】:

我有一个数据框 (df),在数据框内我有一列 user_id

df = sc.parallelize([(1, "not_set"),
                     (2, "user_001"),
                     (3, "user_002"),
                     (4, "n/a"),
                     (5, "N/A"),
                     (6, "userid_not_set"),
                     (7, "user_003"),
                     (8, "user_004")]).toDF(["key", "user_id"])

df:

+---+--------------+
|key|       user_id|
+---+--------------+
|  1|       not_set|
|  2|      user_003|
|  3|      user_004|
|  4|           n/a|
|  5|           N/A|
|  6|userid_not_set|
|  7|      user_003|
|  8|      user_004|
+---+--------------+

我想用 null 替换以下值:not_setn/aN/Auserid_not_set

如果我可以将任何新值添加到列表中并且可以更改它们,那就太好了。

我目前在 spark.sql 中使用 CASE 语句来执行此操作,并希望将其更改为 pyspark。

【问题讨论】:

    标签: apache-spark pyspark apache-spark-sql


    【解决方案1】:

    您可以使用内置的when 函数,它等效于case 表达式。

    from pyspark.sql import functions as f
    df.select(df.key,f.when(df.user_id.isin(['not_set', 'n/a', 'N/A']),None).otherwise(df.user_id)).show()
    

    还可以将所需的值存储在list 中并被引用。

    val_list = ['not_set', 'n/a', 'N/A']
    df.select(df.key,f.when(df.user_id.isin(val_list),None).otherwise(df.user_id)).show()
    

    【讨论】:

    • 我得到这个错误“name 'null' is not defined”,如果我将 null 更改为字符串,那么它可以工作。
    【解决方案2】:

    when() 函数内部的None 对应于null。如果您想填写其他内容而不是 null,则必须将其填写在相应位置。

    from pyspark.sql.functions import col    
    df =  df.withColumn(
        "user_id",
        when(
            col("user_id").isin('not_set', 'n/a', 'N/A', 'userid_not_set'),
            None
        ).otherwise(col("user_id"))
    )
    df.show()
    +---+--------+
    |key| user_id|
    +---+--------+
    |  1|    null|
    |  2|user_001|
    |  3|user_002|
    |  4|    null|
    |  5|    null|
    |  6|    null|
    |  7|user_003|
    |  8|user_004|
    +---+--------+
    

    【讨论】:

    • 我想指出when 如果条件失败并且没有提供otherwise 则返回null。所以在这种情况下,以下是等价的,但更简洁一点:df.withColumn("user_id", when(~col("user_id").isin('not_set', 'n/a', 'N/A', 'userid_not_set'), col("user_id")))
    【解决方案3】:

    PFB 几种方法。我假设所有合法用户 ID 都以"user_" 开头。请尝试以下代码。

    from pyspark.sql.functions import *
    df.withColumn(
        "user_id",
        when(col("user_id").startswith("user_"),col("user_id")).otherwise(None)
    ).show()
    

    另一个。

    cond = """case when user_id in ('not_set', 'n/a', 'N/A', 'userid_not_set') then null
                    else user_id
                end"""
    
    df.withColumn("ID", expr(cond)).show()
    

    另一个。

    cond = """case when user_id like 'user_%' then user_id
                    else null
                end"""
    
    df.withColumn("ID", expr(cond)).show()
    

    另一个。

    df.withColumn(
        "user_id",
        when(col("user_id").rlike("user_"),col("user_id")).otherwise(None)
    ).show()
    

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 1970-01-01
      • 2021-12-13
      • 2018-03-09
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 2020-09-06
      相关资源
      最近更新 更多