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>I would just apply a lambda function to do the operation you want to do (split first on ";", then split on "::" and keep the first element, and join them back):</p>

<pre><code>df['new_col'] = df['old_col'].apply(lambda s: ", ".join(t.split("::")[0] for t in s.split(";")))
</code></pre>

<p>You could also avoid splitting on <code>::</code> since simply stopping before the first <code>:</code> is enough:</p>

<pre><code>df['new_col'] = df['old_col'].apply(lambda s: ", ".join(t[:t.index(":")] for t in s.split(";")))
</code></pre>

python regex pandas text