Double IIF Statement in Python
<p>I have an SQL code that has a double IIF statement in it, that is, the if true part is another IIF statement and I m trying to recreate this in python using <code>np.where</code></p>

<p>the SQL code is:</p>

<pre><code>IIF ([STATE] Is Null, IIF ([COUNTRY] Is Null,'No Country',[COUNTRY]), [STATE])
</code></pre>

<p>I've tried this:</p>

<pre><code>DF['NewCol'] = np.where(DF['STATE'].isnull(),(np.where(DF['COUNTRY'].isnull(),'No Country','DF['COUNTRY']'),DF['STATE']))
</code></pre>

<p>any help appreciated</p>

<p>I think you need:</p>

<pre><code>DF['NewCol'] = np.where(DF['STATE'].isnull(),
               np.where(DF['COUNTRY'].isnull(),'No Country', DF['COUNTRY']),DF['STATE'])
</code></pre>

<p>Sample:</p>

<pre><code>DF = pd.DataFrame({'STATE':[np.nan,'a','b',np.nan],
                   'COUNTRY':[np.nan,'c',np.nan, 'd']})

print (DF)
  COUNTRY STATE
0     NaN   NaN
1       c     a
2     NaN     b
3       d   NaN

DF['NewCol'] = np.where(DF['STATE'].isnull(),
               np.where(DF['COUNTRY'].isnull(),'No Country', DF['COUNTRY']),DF['STATE'])

print (DF)
  COUNTRY STATE      NewCol
0     NaN   NaN  No Country
1       c     a           a
2     NaN     b           b
3       d   NaN           d
</code></pre>

<p>If need first in output column values of <code>COUNTRY</code> column:</p>

<pre><code>DF['NewCol'] = np.where(DF['COUNTRY'].isnull(),
               np.where(DF['STATE'].isnull(),'No Country', DF['STATE']),DF['COUNTRY'])

print (DF)
  COUNTRY STATE      NewCol
0     NaN   NaN  No Country
1       c     a           c
2     NaN     b           b
3       d   NaN           d
</code></pre>

python sql pandas if-statement