python - 如何在 PySpark 中的每个分区中回填空值
问题描述
我在 PySpark 中有以下 DataFrame:
Id DateActual DateStart DateEnd SourceCode
107 2019-08-11 00:00:00 null null 1111
107 2019-08-16 00:00:00 2019-08-11 00:00:00 2019-08-18 00:00:00 1111
128 2019-02-11 00:00:00 null null 101
128 2019-02-13 00:00:00 2019-02-11 00:00:00 2019-02-18 00:00:00 168
128 2019-02-14 00:00:00 2019-02-13 00:00:00 2019-02-20 00:00:00 187
我需要替换null
值以获得以下结果:
Id DateActual DateStart DateEnd SourceCode
107 2019-08-11 00:00:00 2019-08-11 00:00:00 2019-08-18 00:00:00 1111
107 2019-08-16 00:00:00 2019-08-11 00:00:00 2019-08-18 00:00:00 1111
128 2019-02-11 00:00:00 2019-02-11 00:00:00 2019-02-18 00:00:00 101
128 2019-02-13 00:00:00 2019-02-11 00:00:00 2019-02-18 00:00:00 168
128 2019-02-14 00:00:00 2019-02-13 00:00:00 2019-02-20 00:00:00 187
基本上,DateStart
andDateEnd
的null
值等于NEXTDateStart
行的and DateEnd
,如果它具有相同的.Id
如何null
在 PySpark 中按照上述逻辑填写值?
数据框:
df = (
sc.parallelize([
(107, "2019-08-11 00:00:00", None, None, 1111),
(107, "2019-08-16 00:00:00", "2019-08-11 00:00:00", "2019-08-18 00:00:00", 1111),
(128, "2019-02-11 00:00:00", None, None, 101),
(128, "2019-02-13 00:00:00", "2019-02-11 00:00:00", "2019-02-11 00:00:00", 168),
(128, "2019-02-14 00:00:00", "2019-02-13 00:00:00", "2019-02-20 00:00:00", 187)
]).toDF(["Id", "DateActual", "DateStart", "DateEnd", "SourceCode"])
)
这是我尝试过的:
from pyspark.sql.functions import col, when
import pyspark.sql.functions as F
from pyspark.sql.window import Window
my_window = Window.partitionBy("Id").orderBy("DateActual")
df.withColumn("DateStart_start", when(col("DateStart").isNull(), F.lag(df.DateStart).over(my_window)).otherwise(col("DateStart"))).show()
我不需要一个简单的解决方案,因为df.na.fill(0)
. 我需要null
用 NEXT ROW 值替换值,这可能假设使用lag
或其他类似功能。
解决方案
使用first
来自pyspark.sql.functions
:
from pyspark.sql import Window
from pyspark.sql.functions import first
# define the window
window = Window.partitionBy('Id')\
.orderBy('DateActual')\
.rowsBetween(0,sys.maxsize)
# define the back-filled column
filled_column_start = first(spark_df['DateStart'], ignorenulls=True).over(window)
filled_column_end = first(spark_df['DateEnd'], ignorenulls=True).over(window)
# do the fill
spark_df_filled = spark_df.withColumn('filled_start', filled_column_start)
spark_df_filled = spark_df_filled .withColumn('filled_end', filled_column_end)
# show off our glorious achievements
spark_df_filled.orderBy('Id').show(10)
推荐阅读
- java - 运算符 / 未定义参数类型 float,可选
- google-cloud-platform - 无法通过 SSH 连接到 Google Cloud Engine。引导错误
- r - 并排堆叠的不同颜色的条形图
- c++ - 这是升级标准库锁的有效方法吗?
- ajax - 在 Wordpress 上使用 ajax 的注册表单
- javascript - 从字节数组在javascript中创建文件对象
- postgresql - postgresql 性能 - 子查询后分组
- reactjs - 你能用 Redux Toolkit + Immer 改变有效载荷吗?
- amazon-web-services - AWS CloudFormation 忽略元数据中的包
- matlab - 以相等的递增间隔生成随机数