2
votes

How can I create a UDF to programatically replace null values in a spark dataframe in each column with the column mean value. for instance in the example data col1 null value will have a value of ((2+4+6+8+5)/5) = 5.

Example data:

col1    col2    col3
2       null    3
4       3       3
6       5       null
8       null    2
null    6       4
5       2       8

Desired Data:

col1    col2    col3
2       4       3
4       3       3
6       5       4
8       4       2
5       6       4
5       2       8
1
in Pure SQl this could be accomplished by cross joining a table for each column and using coalesce(col1, crossJoinTBL.Col1Avg) but that's not really a UDF. if you were to pass in table column and use dynamic SQL to calculate the avg and use coalesce again that may work... - xQbert

1 Answers

4
votes

Generally speaking there is no need for UDF here. All you really is aggregated table:

val df = Seq(
  (Some(2), None, Some(3)), (Some(4), Some(3), Some(3)),
  (Some(6), Some(5), None), (Some(8), None, Some(2)),
  (None, Some(6), Some(4)), (Some(5), Some(2), Some(8))
).toDF("col1", "col2", "col3").alias("df")

val means = df.agg(df.columns.map(c => (c -> "avg")).toMap)

And broadcasted Cartesian with coalesce:

val exprs = df.columns.map(c => coalesce(col(c), col(s"avg($c)")).alias(c))

df.join(broadcast(means)).select(exprs: _*)