How to get match result by given range using regular expression?
<p>I'm stucking with my code to get all return match by given range. My data sample is:</p>

<pre><code>        comment
0       [intj74, you're, whipping, people, is, a, grea...
1       [home, near, kcil2, meniaga, who, intj47, a, l...
2       [thematic, budget, kasi, smooth, sweep]
3       [budget, 2, intj69, most, people, think, of, e...
</code></pre>

<p>I want to get the result as: (where the given range is intj1 to intj75)</p>

<pre><code>         comment
0        [intj74]   
1        [intj47]    
2        [nan]   
3        [intj69]
</code></pre>

<p>My code is:</p>

<pre><code>df.comment = df.comment.apply(lambda x: [t for t in x if t=='intj74'])
df.ix[df.comment.apply(len) == 0, 'comment'] = [[np.nan]]
</code></pre>

<p>I'm not sure how to use regular expression to find the range for t=='range'. Or any other idea to do this?</p>

<p>Thanks in advance,</p>

<p>Pandas Python Newbie</p>

<p>you could replace <code>[t for t in x if t=='intj74']</code> with, e.g.,</p>

<pre><code>[t for t in x if re.match('intj[0-9]+$', t)]
</code></pre>

<p>or even</p>

<pre><code>[t for t in x if re.match('intj[0-9]+$', t)] or [np.nan]
</code></pre>

<p>which would also handle the case if there are no matches (so that one wouldn't need to check for that explicitly using <code>df.ix[df.comment.apply(len) == 0, 'comment'] = [[np.nan]]</code>) The "trick" here is that an empty list evaluates to <code>False</code> so that the <code>or</code> in that case returns its right operand.</p>

python pandas