Setting values in one dataframe from the boolean values in another
<p>I have a MWE that can be reproduced with the following code:</p>

<pre><code>import pandas as pd
a = pd.DataFrame([[1,2],[3,4]], columns=['A', 'B'])
b = pd.DataFrame([[True,False],[False,True]], columns=['A', 'B'])
</code></pre>

<p>Which creates the following dataframes:</p>

<pre><code>In [8]: a
Out[8]: 
   A  B
0  1  2
1  3  4

In [9]: b
Out[9]: 
       A      B
0   True  False
1  False   True
</code></pre>

<p>My question is, how can I change the values for dataframe <code>A</code> based on the boolean values in dataframe <code>B</code>?</p>

<p>Say for example if I wanted to make <code>NAN</code> values in dataframe <code>A</code> where there's an instance of <code>False</code> in dataframe <code>B</code>?</p>

<p>If need replace <code>False</code> to <code>NaN</code>:</p>

<pre><code>print (a[b])
     A    B
0  1.0  NaN
1  NaN  4.0
</code></pre>

<p>or:</p>

<pre><code>print (a.where(b))
     A    B
0  1.0  NaN
1  NaN  4.0
</code></pre>

<p>and if need replace <code>True</code> to <code>NaN</code>:</p>

<pre><code>print (a[~b])
     A    B
0  NaN  2.0
1  3.0  NaN
</code></pre>

<p>or:</p>

<pre><code>print (a.mask(b))
     A    B
0  NaN  2.0
1  3.0  NaN
</code></pre>

<p>Also you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.where.html" rel="nofollow"><code>where</code></a> or <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.mask.html" rel="nofollow"><code>mask</code></a> with some scalar value:</p>

<pre><code>print (a.where(b, 7))
   A  B
0  1  7
1  7  4

print (a.mask(b, 7))
   A  B
0  7  2
1  3  7

print (a.where(b, 'TEST'))
      A     B
0     1  TEST
1  TEST     4
</code></pre>

python pandas indexing dataframe boolean