Filter down dataframe
<p>I want to filter down a dataframe. Trying to use the standard boolean (multiple) expressions but it doesn't work. My code is:</p>

<pre><code>import pandas as pd
import numpy as np

# Setting a dataframe
dates = pd.date_range('1/1/2000', periods=10)

df1 = pd.DataFrame(np.random.randn(10, 4), index=dates, columns=['A', 'B', 'C', 'D'])

# Filtering
df1 = df1.loc[lambda df: -0.5 &lt; df1.A &lt; 0 and 0 &lt;= df1.B &lt;= 1, :]
</code></pre>

<p>Any thoughts on it?</p>

<p>No need for the anonymous lambda function. Simply filter with a boolean statement. Also, notice the use of the bitwise operator, <code>&amp;</code>, not boolean operator, <code>and</code>. Below are equivalent variants to filtering:</p>

<pre><code>df1 = df1.query('(A &gt; -0.5) &amp; (A &lt; 0) &amp; (B &gt;= 0) &amp; (B &lt;= 1)', engine='python'))

df1 = df1.loc[(df1.A &gt; -0.5) &amp; (df1.A &lt; 0) &amp; (df1.B &gt;= 0) &amp; (df1.B &lt;= 1)]

df1 = df1[(df1.A &gt; -0.5) &amp; (df1.A &lt; 0) &amp; (df1.B &gt;= 0) &amp; (df1.B &lt;= 1)]
</code></pre>

<p>Consider even using <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.between.html" rel="nofollow">pandas.Series.between</a>:</p>

<pre><code>df1 = df1.loc[(df1.A.between(-0.5, 0, inclusive=False) &amp; (df1.B.between(0, 1))]
</code></pre>

python dataframe