I want to convert a spark dataframe that looks like this below
sdf = spark.createDataFrame(pd.DataFrame([[1, 'name_value', None, 'square', 'polygon'],
[2, 'name_value', 5.5, None, 'polygon']],
columns=['id', 'name', 'area', 'sub_type', 'type']))
Fig 1
to look like Fig 2
essentially trying to de-duplicate some records be selecting the best available values (mostly just non-null) values for given name column
I am trying to use the window operations to get to it but am seeing some weird behavior with float column's NaN values
from pyspark.sql.functions import col
from pyspark.sql import functions as F, Window
name_window = Window.partitionBy('name')
(sdf
.withColumn('min_Id', F.min('id').over(name_window))
.withColumn('prefered_area', F.first(col('area')).over(name_window.orderBy(col('area').desc_nulls_last())))
.withColumn('prefered_sub_type', F.first('sub_type').over(name_window.orderBy(col('type').desc_nulls_last())))
.withColumn('area_list', F.collect_list(col('area')).over(name_window.orderBy(col('area'))))
.withColumn('sub_type_list', F.collect_list('sub_type').over(name_window.orderBy(col('type'))))
.withColumn('prefered_type', F.first('type').over(name_window.orderBy(col('type').desc_nulls_last())))
.toPandas())
Fig 3
See how the first and collect_list functions work fine for string columns but creates different results for float columns? like for the string column the prefered_sub_type column has the desired square value but the float column prefered_area is NaN. I also included the collect_list results to show the difference between the float and string columns.
Can someone please take a look and let me know what's the reason for this? and how can I get to my desired dataframe in Fig 2?



