【问题标题】:How to fill out the null values of a column with the first non null value of that column in PySpark如何在 PySpark 中用该列的第一个非空值填充该列的空值
【发布时间】:2021-03-16 12:04:48
【问题描述】:

我正在尝试使用同一列的第一个非空值填充数据框列的空值。

我要填写的数据框是这样的,我希望 'id_book' 列的所有行都具有相同的编号

我尝试了以下方法,但它仍然显示空值

w = Window.partitionBy('id_book').orderBy('id_book', 'date').rowsBetween(0,sys.maxsize)
filled_column = first(spark_df['id_book'], ignorenulls=True).over(w)
spark_df_filled = union_dias.withColumn('id_book_filled_spark', filled_column)

【问题讨论】:

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


    【解决方案1】:

    窗口应该是

    w = Window.orderBy('date').rowsBetween(0, Window.unboundedFollowing)
    filled_column = first(spark_df['id_book'], ignorenulls=True).over(w)
    spark_df_filled = spark_df.withColumn('id_book_filled_spark', filled_column)
    

    因为您不想按id_book 进行分区。也没有必要通过id_book 订购,因为只有日期的顺序很重要。

    另外我认为更好的做法是使用Window.unboundedFollowing 而不是sys.maxsize。

    【讨论】:

      猜你喜欢
      • 2018-11-30
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      • 2016-10-11
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      相关资源
      最近更新 更多