collect rows and columns based on id matching
<p>I use python and the pandas library. I want to collect the rows and columns from a data-frame according to one criteria, collect only those ids with a pattern like 'BIKE-\d\d\d\d' from a specific column 'BikeID'. I tried several versions of the following: </p>

<p>d1 = pandas.dataframe</p>

<pre><code>d2 = d1[d1["BikeID"] == re.compile(r' (BIKE-\d\d\d\d)')] 
</code></pre>

<p>but I am getting an empty data-frame instead. It works when it is specific: </p>

<pre><code>d2 = d1[d1["BikeID"] == 'BIKE-0001']
</code></pre>

<p>,but I want to match all ids that have BIKE in the front. I would appreciate if you could show me a way of doing this task.    </p>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.extract.html" rel="nofollow"><code>str.extract</code></a> to achieve this with the regex pattern <code>'(^BIKE-[\d]{4})'</code> this will look for strings that start with BIKE- and then 4 digits:</p>

<pre><code>In [167]:
s= pd.Series(['BIKE-0001', 'BIKE','BIKE-000','sdBIKE-0001'])
s

Out[167]:
0      BIKE-0001
1           BIKE
2       BIKE-000
3    sdBIKE-0001
dtype: object

In [168]:
s.str.extract(r'(^BIKE-[\d]{4})', expand=False)

Out[168]:
0    BIKE-0001
1          NaN
2          NaN
3          NaN
dtype: object
</code></pre>

python pandas dataframe