title: How to iteratively build F.when().otherwise logic
body: <p>I'm trying to programmatically build out a series of F.when().otherwise() conditional statements using pyspark. Pretty much I want to host layers of &quot;decisions&quot; in a dictionary, and programmatically build a series of F.when().otherwise() statements. An example of what I'm trying to achieve should make this clear...</p>
  <p>At the moment I have this dictionary of decisions...</p>
  <pre><code>from pyspark.sql import functions as F
  
  decision = {
      &quot;first_layer&quot;: {
          &quot;rule&quot;: F.col(&quot;col1&quot;) * 25 &gt; 0.34,
          &quot;outcome&quot;: 0,
      },
      &quot;second_layer&quot;: {
          &quot;rule&quot;: F.col(&quot;col2&quot;) * 100 &gt; 0.34,
          &quot;outcome&quot;: 0,
      },
      &quot;last_layer&quot;: {
          &quot;rule&quot;: F.col(&quot;col3&quot;) &gt; 0.35,
          &quot;outcome&quot;: 1,
          &quot;termination_criteria&quot;: 0,
      },
  }
  </code></pre>
  <p>That I could manually pass with the below in the F.when().otherwise() series...</p>
  <pre><code>rule_1 = decision[&quot;first_layer&quot;][&quot;rule&quot;]
  outcome_1 = decision[&quot;first_layer&quot;][&quot;outcome&quot;]
  
  rule_2 = decision[&quot;second_layer&quot;][&quot;rule&quot;]
  outcome_2 = decision[&quot;second_layer&quot;][&quot;outcome&quot;]
  
  rule_3 = decision[&quot;last_layer&quot;][&quot;rule&quot;]
  outcome_3 = decision[&quot;last_layer&quot;][&quot;outcome&quot;]
  termination_criteria = decision[&quot;last_layer&quot;][&quot;termination_criteria&quot;]
  </code></pre>
  <p>I want to be able to build the below logic programatically instead of having to type it out...</p>
  <pre><code>F.when(rule_1, outcome_1).otherwise(
      F.when(rule_2, outcome_2).otherwise(
          F.when(rule_3, outcome_3).otherwise(termination_criteria)
      )
  ),
  </code></pre>
  <p>Does anybody have any suggestions as to how I could loop over the <code>decision</code> dictionary and programmatically build the F.when().otherwise() series? The idea is I want to make the construction of the F.when().otherwise() series generalisable, so I could pass in different decision dictionaries and build the conditional logic, where each decision dictionary will always have a &quot;last_layer&quot;</p>
  <p>I've tried a recursive function to try and get this working, but I couldn't get it to work...</p>
  <p>Anyone have any advice here?</p>