我有一个 pyspark 数据框
deviceId timestamp
009eeb 2024-04-22
009eeb 2024-04-24
7c002v 2024-04-20
7c002v null
4fd556 null
4fd556 null
我需要获取最终数据帧中的最大时间戳并删除重复项。 我尝试使用下面的代码
w = Window.partitionBy('deviceId')
df_max = df.withColumn('max_col', F.max('timestamp').over(w))\
.where((F.col('timestamp') == F.col('max_col')) | (F.col('timestamp').isNull()))
.dropDuplicates()
但是从这段代码中,我得到了我需要得到的东西。
deviceId timestamp
009eeb 2024-04-24
7c002v null
4fd556 null
4fd556 null
我需要得到如下结果
deviceId timestamp
009eeb 2024-04-24
7c002v 2024-04-20
4fd556 null
请提出任何需要进行的更改
直接使用
max
功能即可。
from pyspark.sql import functions as F
...
df = df.groupBy('deviceId').agg(F.max('timestamp').alias('timestamp'))