Parse pandas df column with regex extracting substrings
<p>I have a pandas df containing a column composed of text like:</p>

<pre><code>String1::some_text::some_text;String2::some_text::;String3::some_text::some_text;String4::some_text::some_text
</code></pre>

<p>I can see that:</p>

<ol>
<li>The start of the text always contains the first string I want to extract</li>
<li>The rest of the strings are in between "::" and ";" </li>
</ol>

<p>I want to create a new column containing:</p>

<pre><code>String1, String2, String3, String4
</code></pre>

<p>All separed by a comma but still in the same column. </p>

<p>How to approach the problem?</p>

<p>Thanks for your help</p>

<p>try this:</p>

<pre><code>In [136]: df.txt.str.findall(r'String\d+').str.join(', ')
Out[136]:
0    String1, String2, String3, String4
Name: txt, dtype: object
</code></pre>

<p>Data:</p>

<pre><code>In [137]: df
Out[137]:
                                                                                                   txt
0  String1::some_text::some_text;String2::some_text::;String3::some_text::some_text;String4::some_t...
</code></pre>

<p>Setup:</p>

<pre><code>df = pd.DataFrame({'txt': ['String1::some_text::some_text;String2::some_text::;String3::some_text::some_text;String4::some_text::some_text']})
</code></pre>

python regex pandas text