How to create filtered DataFrame with minimum code
<p>There are four cars: <code>bmw</code>, <code>geo</code>, <code>vw</code> and <code>porsche</code>:</p>

<pre><code>import pandas as pd
df = pd.DataFrame({
    'car':      ['bmw','geo','vw','porsche'],
    'warranty': ['yes','yes','yes','no'], 
    'dvd':      ['yes','yes','no','yes'], 
    'sunroof':  ['yes','no','no','no']})
</code></pre>

<p><a href="http://i.stack.imgur.com/Mq1F2.png" rel="nofollow"><img src="http://i.stack.imgur.com/Mq1F2.png" alt="enter image description here"></a></p>

<p>I would like to create a filtered DataFrame that lists only those cars that have all three features presented: the DVD player, the sunroof and a warranty (we know it is BMW here that has all features set to 'yes').</p>

<p>I can do one column at time with:</p>

<pre><code>cars_with_warranty = df['car'][df['warranty']=='yes']
print(cars_with_warranty)
</code></pre>

<p><a href="http://i.stack.imgur.com/hz3mn.png" rel="nofollow"><img src="http://i.stack.imgur.com/hz3mn.png" alt="enter image description here"></a></p>

<p>Then I need to do a similar column calculation for dvd and sunroof columns:</p>

<pre><code>cars_with_dvd = df['car'][df['dvd']=='yes']
cars_with_sunroof = df['car'][df['sunroof']=='yes']
</code></pre>

<p>I wonder if there is a clever way of creating the filtered <code>DataFrame</code>?</p>

<h3>EDITED LATER:</h3>

<p>The posted solution works well. But the resulting <code>cars_with_all_three</code> is a simple list variable. We need the DataFrame object with a single 'bmw' car as its the only row and all three columns in place: dvd, sunroof and warranty (with all three values set to 'yes').</p>

<pre><code>cars_with_all_three = []
for ind, car in enumerate(df['car']):
    if df['dvd'][ind] == df['warranty'][ind] == df['sunroof'][ind] == 'yes':
        cars_with_all_three.append(car)
</code></pre>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing" rel="nofollow"><code>boolean indexing</code></a>:</p>

<pre><code>print ((df.dvd == 'yes') &amp; (df.sunroof == 'yes') &amp; (df.warranty == 'yes'))
0     True
1    False
2    False
3    False
dtype: bool

print (df[(df.dvd == 'yes') &amp; (df.sunroof == 'yes') &amp; (df.warranty == 'yes')])
   car  dvd sunroof warranty
0  bmw  yes     yes      yes

#if need filter only column 'car' 
print (df.ix[(df.dvd == 'yes')&amp;(df.sunroof == 'yes')&amp;(df.warranty == 'yes'), 'car'])
0    bmw
Name: car, dtype: object
</code></pre>

<p>Another solution with checking if all values in columns are <code>yes</code> and then check if all values are <code>True</code> by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.all.html" rel="nofollow"><code>all</code></a>:</p>

<pre><code>print ((df[[ u'dvd', u'sunroof', u'warranty']] == "yes").all(axis=1))
0     True
1    False
2    False
3    False
dtype: bool

print (df[(df[[ u'dvd', u'sunroof', u'warranty']] == "yes").all(axis=1)])
   car  dvd sunroof warranty
0  bmw  yes     yes      yes

print (df.ix[(df[[ u'dvd', u'sunroof', u'warranty']] == "yes").all(axis=1), 'car'])
0    bmw
Name: car, dtype: object
</code></pre>

<p>Solution with minimal code, if <code>DataFrame</code> has only <code>4</code> columns like sample:</p>

<pre><code>print (df[(df.set_index('car') == 'yes').all(1).values])
   car  dvd sunroof warranty
0  bmw  yes     yes      yes
</code></pre>

<p><strong>Timings</strong>:</p>

<pre><code>In [44]: %timeit ([car for ind, car in enumerate(df['car']) if df['dvd'][ind] == df['warranty'][ind] == df['sunroof'][ind] == 'yes'])
10 loops, best of 3: 120 ms per loop

In [45]: %timeit (df[(df.dvd == 'yes')&amp;(df.sunroof == 'yes')&amp;(df.warranty == 'yes')])
The slowest run took 4.39 times longer than the fastest. This could mean that an intermediate result is being cached.
100 loops, best of 3: 2.09 ms per loop

In [46]: %timeit (df[(df[[ u'dvd', u'sunroof', u'warranty']] == "yes").all(axis=1)])
1000 loops, best of 3: 1.53 ms per loop

In [47]: %timeit (df[(df.ix[:, [u'dvd', u'sunroof', u'warranty']] == "yes").all(axis=1)])
The slowest run took 4.46 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 1.51 ms per loop

In [48]: %timeit (df[(df.set_index('car') == 'yes').all(1).values])
1000 loops, best of 3: 1.64 ms per loop

In [49]: %timeit (mer(df))
The slowest run took 4.17 times longer than the fastest. This could mean that an intermediate result is being cached.
100 loops, best of 3: 3.85 ms per loop
</code></pre>

<p><strong>Code for timings</strong>:</p>

<pre><code>df = pd.DataFrame({
    'car':      ['bmw','geo','vw','porsche'],
    'warranty': ['yes','yes','yes','no'], 
    'dvd':      ['yes','yes','no','yes'], 
    'sunroof':  ['yes','no','no','no']})

print (df)
df = pd.concat([df]*1000).reset_index(drop=True)

def mer(df):
    df = df.set_index('car')
    return df[df[[ u'dvd', u'sunroof', u'warranty']] == "yes"].dropna().reset_index()
</code></pre>

python pandas indexing dataframe condition