Pyspark update two columns using one when statement?
<p>So I am using <code>df.Withcolumn()</code> in PySpark to create a column and using <code>F.when()</code> to specify the criteria as to when the column should be updated.</p>

<pre><code>df = df.withColumn('ab', F.when(df['text']=="0", 1).otherwise(0))
</code></pre>

<p>Basically I am updating the column to be '1' if it matches the criteria. Now, I want to update another column in the same <code>df</code> if the same criteria matches (eg. <code>df['text']=="0"</code>). Is there any way in PySpark to update two columns using one when statement?</p>

<p>It is not possible. You can only created struct:</p>

<pre><code>&gt;&gt;&gt; from pyspark.sql.functions import *
&gt;&gt;&gt; df.withColumn('ab', F.when(df['text']=="0" , struct(1, "foo")).otherwise(struct(0, "bar")))
</code></pre>

python pyspark