Match a substring in Pandas wit str.extract method
<p>I have a string looking like:</p>

<pre><code>29818-218705-61709-2    
</code></pre>

<p>I want to extract the second to last 5 digits number between the two dashes</p>

<pre><code>61709
</code></pre>

<p>each string is contained in a pandas series:</p>

<p>I came up with:</p>

<pre><code>df.id.str.extract(r'[.-]([0-9]{5})[.-]?')
</code></pre>

<p>but it extracts the first 5 digits group.</p>

<p>I can I match the one I want?</p>

<p>you can use <code>split</code></p>

<pre><code>df.id.str.split('-').str[-2]
</code></pre>

<hr>

<p><strong><em>demo</em></strong>  </p>

<pre><code>df = pd.DataFrame(dict(id=['29818-218705-61709-2'] * 1000)) 
df.id.str.split('-').str[-2].head()

0    61709
1    61709
2    61709
3    61709
4    61709
Name: id, dtype: object
</code></pre>

python regex string pandas