How to update column based on a condition (a value in a group)?
<p>I have the following df: </p>

<pre><code>+---+----+-----+
|sno|dept|color|
+---+----+-----+
|  1|  fn|  red|
|  2|  fn| blue|
|  3|  fn|green|
+---+----+-----+
</code></pre>

<p>If any of the color column values is <code>red</code>, then I all values of the color column should be updated to be <code>red</code>, as below: </p>

<pre><code>+---+----+-----+
|sno|dept|color|
+---+----+-----+
|  1|  fn|  red|
|  2|  fn|  red|
|  3|  fn|  red|
+---+----+-----+
</code></pre>

<p>I could not figure it out. Please help; I have tried following code: </p>

<pre><code>val gp=jdbcDF.filter($"dept".contains("fn"))
     //.withColumn("newone",when($"dept"==="fn","RED").otherwise("NULL"))
    gp.show()
gp.map(
  row=&gt;{
    val row1=row.getAs[String](1)
    var row2=row.getAs[String](2)
    val make=if(row1 =="fn") row2="red"
    Row(row(0),row(1),make)
  }
).collect().foreach(println)
</code></pre>