title: Why this nested "when" does not work in pyspark?
body: <p>I'm trying to divide people into age range with</p>
  <pre><code>from pyspark import SparkFiles
  from pyspark.sql import functions as fn
  
  ## Import data
  
  url_users = &quot;https://raw.githubusercontent.com/leanhdung1994/BigData/main/users.csv&quot;
  spark.sparkContext.addFile(url_users)
  users_from_file = spark.read.csv(&quot;file://&quot; + SparkFiles.get(&quot;users.csv&quot;), header = True, sep = &quot;,&quot;, inferSchema = True)
  
  ## Generate column age
  
  reference_date = date(2017, 12, 31)
  from pyspark.sql.types import IntegerType
  def cal_age(born):
      return reference_date.year - born.year - ((reference_date.month, reference_date.day) &lt; (born.month, born.day))
  users_from_file = users_from_file.withColumn('age', cal_age_udf(fn.to_date(fn.col('birth_date'))))
  
  ## Generate column range
  
  users_from_file1 = users_from_file.withColumn('range', fn.when(fn.col(&quot;age&quot;) &lt;= 25, 1)fn.when(fn.col(&quot;age&quot;) &lt;= 35, 2).fn.otherwise(3))
  
  users_from_file1.show()
  </code></pre>
  <p>Then it returns an error</p>
  <pre><code>SyntaxError: invalid syntax
    File &quot;&lt;command-2296735704765764&gt;&quot;, line 3
      users_from_file1 = users_from_file.withColumn('range', fn.when(fn.col(&quot;age&quot;) &lt;= 25, 1)fn.when(fn.col(&quot;age&quot;) &lt;= 35, 2).fn.otherwise(3))
                                                                                             ^
  SyntaxError: invalid syntax
  </code></pre>
  <p>Could you please elaborate more on this nested <code>when</code>? This syntax of <code>When</code> is from <a href="https://stackoverflow.com/a/39048475/7357673">this answer</a>, but it does not work.</p>
  <p><a href="https://i.stack.imgur.com/7UT3o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7UT3o.png" alt="enter image description here" /></a></p>