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>consider the dataframe <code>df</code> with column <code>txt</code></p>

<pre><code>df = pd.DataFrame(['String1::some_text::some_text;String2::some_text::;String3::some_text::some_text;String4::some_text::some_text'] * 10,
                  columns=['txt'])
df
</code></pre>

<p><a href="http://i.stack.imgur.com/bMddU.png" rel="nofollow"><img src="http://i.stack.imgur.com/bMddU.png" alt="enter image description here"></a></p>

<hr>

<p>use a combination of <code>str.split</code> and <code>groupby</code></p>

<pre><code>df.txt.str.split(';', expand=True).stack() \
      .str.split('::').str[0].groupby(level=0).apply(list)

0    [String1, String2, String3, String4]
1    [String1, String2, String3, String4]
2    [String1, String2, String3, String4]
3    [String1, String2, String3, String4]
4    [String1, String2, String3, String4]
5    [String1, String2, String3, String4]
6    [String1, String2, String3, String4]
7    [String1, String2, String3, String4]
8    [String1, String2, String3, String4]
9    [String1, String2, String3, String4]
dtype: object
</code></pre>

python regex pandas text