how to use the `query` method to check if elements of a column contain a specific string
<p>I know how to check if a column contains a string.  My preffered method is to use <code>.str.contains</code>.  However, that returns a boolean array that I have to use as a mask on the original dataframe.  The convenience of <code>query</code> is that it returns the already filtered dataframe.</p>

<p>consider the <code>df</code></p>

<pre><code>df = pd.DataFrame(np.array(list('abcdefghijklmno')).reshape(5, 3),
                  columns=list('XYZ')).add('w')
df
</code></pre>

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

<p>Using <code>str.contains</code></p>

<pre><code>df[df.Y.str.contains('b')]
</code></pre>

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

<p>But I have a preference to use <code>query</code></p>

<pre><code>df.query('Y == "bw"')
</code></pre>

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

<p>The problem is, I don't know how to use <code>query</code> to check for substrings.  I wanted something similar to this.</p>

<pre><code>df.query('Y like "b%"')
</code></pre>

<p>This is currently not supported, <code>query</code> only implements a subset of operations, basically none of the string functions.</p>

<p>Just a sidenote to the comment, <code>query</code> does support a vectorized version of the <code>in</code> keyword.</p>

<pre><code>df.query('X in ["aw", "dw"]')
Out[9]: 
    X   Y   Z
0  aw  bw  cw
1  dw  ew  fw
</code></pre>

python pandas