Q:

Selecting columns whose names match regex

How can one use a logical index (or any other efficient method) to select columns for which the column name contains a certain match to a regular expression.
For example,
raw = '''   id 0_date 0_hr 1_date 1_hr
1 a 21-Jan      30 2-Mar      75
'''

import pandas as pd
from StringIO import StringIO
df = pd.read_table(StringIO(raw),header=0,index_col=[0],sep="\s+")

I would like to create a new dataframe with only the id column and all columns that contain the string "date". I was not able to use str.contains on df.columns. It seems the filter function works, but I wanted to create the logical index if that is a valid method. Thanks.

A:

If you convert the column Index object to a series, you can use .str to perform vectorized string operations (like regex searches):
>>> df.columns
Index([u'id', u'0_date', u'0_hr', u'1_date', u'1_hr'], dtype='objec
>>> df.columns.to_series().str
<pandas.core.strings.StringMethods object at 0xa2b56cc>
>>> df.columns.to_series().str.contains("date")
id        False
0_date     True
0_hr      False
1_date     True
1_hr      False
dtype: bool
>>> df.loc[:, df.columns.to_series().str.contains("date")]
   0_date 1_date
1  21-Jan  2-Mar

In this case, I might use endswith:
>>> df.loc[:, df.columns.to_series().str.endswith("date")]
   0_date 1_date
1  21-Jan  2-Mar

(Personally, I think Index objects should grow a .str which is basically .to_series().str, to make this a little cleaner.)

