How to do logical operation between DataFrame and Series?
<p>Suppose I have a bool <code>DataFrame df</code> and a bool <code>Series x</code> with the same index, and I want to do logical operation between <code>df</code> and <code>x</code> per column. Is there any short and fast way like <code>DataFrame.sub</code> compare to using <code>DataFrame.apply</code>?</p>

<pre><code>In [31]: df
Out[31]: 
       x      y      z      u
A  False  False   True   True
B   True   True   True   True
C   True  False  False  False

In [32]: x
Out[32]: 
A     True
B    False
C     True
dtype: bool

In [33]: r = df.apply(lambda col: col &amp; x) # Any other way ??

In [34]: r
Out[34]: 
       x      y      z      u
A  False  False   True   True
B  False  False  False  False
C   True  False  False  False
</code></pre>

<p>Use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.mul.html" rel="nofollow"><code>mul</code></a>, but need cast to <code>int</code> and then to <code>bool</code>, because <code>UserWarning</code>:</p>

<pre><code>print (df.astype(int).mul(x.values, axis=0).astype(bool))
       x      y      z      u
A  False  False   True   True
B  False  False  False  False
C   True  False  False  False
</code></pre>

<p>Similar solution:</p>

<pre><code>print (df.mul(x.astype(int), axis=0).astype(bool))
       x      y      z      u
A  False  False   True   True
B  False  False  False  False
C   True  False  False  False
</code></pre>

<hr>

<pre><code>print (df.mul(x.values, axis=0))
       x      y      z      u
A  False  False   True   True
B  False  False  False  False
C   True  False  False  False
</code></pre>

<blockquote>
  <p>C:\Anaconda3\lib\site-packages\pandas\computation\expressions.py:181: UserWarning: evaluating in Python space because the '*' operator is not supported by numexpr for the bool dtype, use '&amp;' instead
        unsupported[op_str]))</p>
</blockquote>

<p>Another <code>numpy</code> solution with <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.logical_and.html" rel="nofollow"><code>np.logical_and</code></a>:</p>

<pre><code>print (pd.DataFrame(np.logical_and(df.values, x.values[:, None]), 
                                   index=df.index, 
                                   columns=df.columns))

       x      y      z      u
A  False  False   True   True
B  False  False  False  False
C   True  False  False  False
</code></pre>

python pandas dataframe boolean logical-operators