pandas: extract a subset of columns where the column name contains a string <p>A simple example should make this obvious. Sample data:</p> <pre><code>df = pd.DataFrame( np.random.randn(2,6), columns=['x','y','xy','yx','xx','yy'] ) </code></pre> <p>Now, I just want to list the values for columns containing 'x'. Here's a couple of ways:</p> <pre><code>df[[ x for x in df.columns if 'x' in x ]] Out[53]: x xy yx xx 0 2.089078 1.111139 -0.218800 1.025810 1 -0.343189 0.274676 -0.342798 -0.503809 df[ df.columns[pd.Series(df.columns).str.contains('x')] ] Out[54]: x xy yx xx 0 2.089078 1.111139 -0.218800 1.025810 1 -0.343189 0.274676 -0.342798 -0.503809 </code></pre> <p>The latter approach seems promising but it's just really ugly and I haven't so far found a way to shorten it. Something more like this would be great:</p> <pre><code>df[ columns_with( df, 'x' ) ] </code></pre> <p>and in fact I did something just like that with a function, but am wondering if there is a pandastic way to do this without a user written function or monkeypatch?</p> <p>For motivation/background, this sort of thing is super useful when you have an unfamiliar dataset with lots of columns or even when you have a familiar dataset but can't remember the exact name of one variable out of hundreds. For the situations where I need this functionality, I'll often be doing this over and over again during data exploration stages, so it's really worth it to me to have a quick and simple way to do this.</p>