【问题标题】:Pyspark: How to fill null values based on value on another columnPyspark:如何根据另一列的值填充空值
【发布时间】:2021-12-17 15:33:31
【问题描述】:

我想根据 id 列的值在 Spark df 上填充空值。

Pyspark df:

index id animal name
1 001 cat doug
2 002 dog null
3 001 cat null
4 003 null null
5 001 null doug
6 002 null bob
7 003 bird larry

预期结果:

index id animal name
1 001 cat doug
2 002 dog bob
3 001 cat doug
4 003 bird larry
5 001 cat doug
6 002 dog bob
7 003 bird larry

【问题讨论】:

    标签: pyspark


    【解决方案1】:

    您可以将last(或first)与窗口函数一起使用。

    from pyspark.sql import Window
    from pyspark.sql import functions as F
    
    w = Window.partitionBy('id')
    df = (df.withColumn('animal', F.last('animal', ignorenulls=True).over(w))
          .withColumn('name', F.last('name', ignorenulls=True).over(w)))
          
    

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 2020-02-17
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多