title: Replace Null Values of a Column with mean of another Categorcial Column in Spark Dataframe
body: <p>I have a dataset like this</p>
  
  <pre><code>id    category     value
  1     A            NaN
  2     B            NaN
  3     A            10.5
  5     A            2.0
  6     B            1.0
  </code></pre>
  
  <p>I want to fill the NAN values with the mean of their respective category. As shown below</p>
  
  <pre><code>id    category     value
  1     A            4.16
  2     B            0.5
  3     A            10.5
  5     A            2.0
  6     B            1.0
  </code></pre>
  
  <p>I tried to calculate first mean values of each category using group by</p>
  
  <pre><code>val df2 = dataFrame.groupBy(category).agg(mean(value)).rdd.map{
        case r:Row =&gt; (r.getAs[String](category),r.get(1))
      }.collect().toMap
      println(df2)
  </code></pre>
  
  <p>I got map of each category and their respective mean values.<code>output: Map(A -&gt;4.16,B-&gt;0.5)</code>
   Now i tried update query in Sparksql to fill column but it seems spqrkSql dosnt support update query. I tried to fill null values with in dataframe but failed to do so. 
  What can i do? We can do the same in pandas as shown in <a href="https://stackoverflow.com/questions/40299055/pandas-how-to-fill-null-values-with-mean-of-a-groupby">Pandas: How to fill null values with mean of a groupby?</a>
  But how can i do using spark dataframe</p>