Using two different data frames to compute new variable
<p>I have two dataframes of the same dimensions that look like:</p>

<pre><code>   df1
ID    flag
0      1 
1      0
2      1

  df2
ID    flag
0      0 
1      1
2      0
</code></pre>

<p>In both dataframes I want to create a new variable that denotes an additive flag. So the new variable will look like this:</p>

<pre><code>   df1
ID    flag   new_flag
0      1        1
1      0        1
2      1        1

  df2
ID    flag   new_flag
0      0        1
1      1        1
2      0        1
</code></pre>

<p>So if either flag columns is a <code>1</code> the new flag will be a <code>1</code>. 
I tried this code:</p>

<pre><code>df1['new_flag']= 1
df2['new_flag']= 1

df1['new_flag'][(df1['flag']==0)&amp;(df1['flag']==0)]=0
df2['new_flag'][(df2['flag']==0)&amp;(df2['flag']==0)]=0
</code></pre>

<p>I would expect the same number of <code>1</code> in both <code>new_flag</code> but they differ. Is this because I'm not going row by row? Like this question? 
<a href="http://stackoverflow.com/questions/26886653/pandas-create-new-column-based-on-values-from-other-columns">pandas create new column based on values from other columns</a>
If so how do I include criteria from both datafrmes? </p>

<p>You can use <code>np.logical_or</code> to achieve this, if we set <code>df1</code> to be all 0's except for the last row so we don't just get a column of <code>1</code>'s, we can cast the result of <code>np.logical_or</code> using <code>astype(int)</code> to convert the boolean array to <code>1</code> and <code>0</code>:</p>

<pre><code>In [108]:
df1['new_flag'] = np.logical_or(df1['flag'], df2['flag']).astype(int)
df2['new_flag'] = np.logical_or(df1['flag'], df2['flag']).astype(int)
df1

Out[108]:
   ID  flag  new_flag
0   0     0         0
1   1     0         1
2   2     1         1

In [109]:
df2

Out[109]:
   ID  flag  new_flag
0   0     0         0
1   1     1         1
2   2     0         1
</code></pre>

python pandas