Pandas conditional creation of a new dataframe column
<p>This question is an extension of <a href="http://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column/39224761#39224761">Pandas conditional creation of a series/dataframe column</a>.
If we had this dataframe:</p>

<pre><code>    Col1       Col2
1    A          Z
2    B          Z           
3    B          X
4    C          Y
5    C          W
</code></pre>

<p>and we wanted to do the equivalent of:</p>

<pre><code>if Col2 in ('Z','X') then Col3 = 'J' 
else if Col2 = 'Y' then Col3 = 'K'
else Col3 = {value of Col1}
</code></pre>

<p>How could I do that?</p>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html" rel="nofollow"><code>loc</code></a> with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.isin.html" rel="nofollow"><code>isin</code></a> and last <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.fillna.html" rel="nofollow"><code>fillna</code></a>:</p>

<pre><code>df.loc[df.Col2.isin(['Z','X']), 'Col3'] = 'J'
df.loc[df.Col2 == 'Y', 'Col3'] = 'K'
df['Col3'] = df.Col3.fillna(df.Col1)
print (df)
  Col1 Col2 Col3
1    A    Z    J
2    B    Z    J
3    B    X    J
4    C    Y    K
5    C    W    C
</code></pre>

python pandas if-statement dataframe multiple-columns