Substitute values of different column in dataframe with Pandas
<p>I have one dataframe <code>df_in</code> defined as:</p>

<pre><code>import pandas as pd

dic_in = {'A': ['ff','rr' ,'nn' ,'qq','tt' ,'pp','uu'],
      'B1': ['33',r'\N','39' ,'22',r'\N','56','90'],
      'C1': ['44',r'\N','74' ,'34',r'\N','89','99'],
      'B2': ['33','63' ,r'\N','22','71' ,'56','90'],
      'C2': ['44','85' ,r'\N','34','52' ,'89','99']}
df_in = pd.DataFrame(dic_in,columns=['A','B1','C1','B2','C2'])
</code></pre>

<p>if I print it on the console it looks like this:</p>

<pre><code>In [28]:df_in
Out[28]: 
    A  B1  C1  B2  C2
0  ff  33  44  33  44
1  rr  \N  \N  63  85
2  nn  39  74  \N  \N
3  qq  22  34  22  34
4  tt  \N  \N  71  52
5  pp  56  89  56  89
6  uu  90  99  90  99
</code></pre>

<p>What I would like to do is to investigate every single row of the columns <code>B1</code> and <code>C1</code>: if the generic row contains <code>\N</code> in both columns it needs to substitute its values with the content of <code>B2</code> and <code>C2</code> respectively. In such way the output (<code>df_out</code>) should look like this:</p>

<pre><code>In [28]:df_in                In[30]:df_out
Out[28]:                     Out[30]:
    A  B1  C1  B2  C2            A   B   C
0  ff  33  44  33  44        0  ff  33  44
1  rr  \N  \N  63  85 -----&gt; 1  rr  63  85
2  nn  39  74  \N  \N -----&gt; 2  nn  39  74
3  qq  22  34  22  34        3  qq  22  34
4  tt  \N  \N  71  52 -----&gt; 4  tt  71  52
5  pp  56  89  56  89        5  pp  56  89
6  uu  90  99  90  99        6  uu  90  99
</code></pre>

<p>I was able to achieve my goal using these lines of code:</p>

<pre><code>df_out = pd.DataFrame()
for index, row in df_in.iterrows():
    if row['B1']!=r'\N' and row['C1']!=r'\N':
        dic = {'A': [row['A']], 'B': [row['B1']], 'C': [row['C1']]}
        df_out = pd.concat([df_out,pd.DataFrame(dic)], ignore_index=True)
    else:
        dic = {'A': [row['A']], 'B': [row['B2']], 'C': [row['C2']]}
        df_out = pd.concat([df_out,pd.DataFrame(dic)], ignore_index=True)
</code></pre>

<p>Can you please suggest me a smart way to achieve such result?</p>