Find cells in dataframe where value is between x and y
<p>I want all values in a pandas dataframe as True / False depending on whether the value is between the given x and y.</p>

<p>Any combining of 2 dataframes using an 'AND' operator, or any 'between' functionality from pandas would be nice. I would prefer not to loop over the columns and call the pandas.Series.between(x, y) function.</p>

<p><strong>Example</strong></p>

<p>Given the following dataframe</p>

<pre><code>&gt;&gt;&gt; df = pd.DataFrame([{1:1,2:2,3:6},{1:9,2:9,3:10}])
&gt;&gt;&gt; df
   1  2   3
0  1  2   6
1  9  9  10
</code></pre>

<p>I want all values between x and y. I can for example start with:</p>

<pre><code>&gt;&gt;&gt; df &gt; 2
       1      2     3
0  False  False  True
1   True   True  True
</code></pre>

<p>and then do </p>

<pre><code>&gt;&gt;&gt; df &lt; 10
      1     2      3
0  True  True   True
1  True  True  False
</code></pre>

<p>But then</p>

<pre><code>&gt;&gt;&gt; df &gt; 2 and df &lt; 10
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "C:\Users\Laurens Koppenol\Anaconda2\lib\site-packages\pandas\core\generic.py", line 731, in __nonzero__
    .format(self.__class__.__name__))
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
</code></pre>

<p>use <code>&amp;</code> with parentheses (due to operator precedence), <code>and</code> doesn't understand how to treat an array of booleans hence the warning:</p>

<pre><code>In [64]:
df = pd.DataFrame([{1:1,2:2,3:6},{1:9,2:9,3:10}])
(df &gt; 2) &amp; (df &lt; 10)

Out[64]:
       1      2      3
0  False  False   True
1   True   True  False
</code></pre>

<p>It's possible to use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.between.html" rel="nofollow"><code>between</code></a> with <code>apply</code> but this will be slower for a large df:</p>

<pre><code>In [66]:
df.apply(lambda x: x.between(2,10, inclusive=False))

Out[66]:
       1      2      3
0  False  False   True
1   True   True  False
</code></pre>

<p>Note that this warning will get raised whenever you try to compare a df or series using <code>and</code>, <code>or</code>, and <code>not</code>, you should use <code>&amp;</code>, <code>|</code>, and <code>~</code> respectively as these bitwise operators understand how to treat arrays correctly</p>

python pandas