Filtering a pandas dataframe based on a match to partial strings
<p>I have a pandas dataframe that contains strings of varying length and characters.</p>

<p>For example:</p>

<pre><code>print df['name'][0]
print df['name'][1]
print df['name'][2]
print df['name'][3]
</code></pre>

<p>would return something like this:</p>

<pre><code>UserId : Z5QF1X33A
loginId : test.user
UserId : 0000012348;  searchText : Cap
accountSampleToExclude : 0;  accountSampleName : Sample Text;  UserId : Z5QF1X33A;  accountSampleType : Test;  accountSample : Test
</code></pre>

<p>What I want to do is be able to parse through the column and only return the actual relevant id so based on the above example:</p>

<pre><code>Z5QF1X33A
test.user
0000012348
Z5QF1X33A
</code></pre>

<p>I figured regex would be an easy approach to solving this, but so far I've only been able to come up with some hardcoded pseudo solution to only partial cases:</p>

<pre><code> df['name'] = df['name'].str.strip(r'(?&lt;=\UserId :).*')
 df['name'] = df['name'].str.strip(r'(?&lt;=\loginId :).*')
</code></pre>

<p>That would work for the rows that are similar to </p>

<pre><code>df['name'][0] 
df['name'][1]
</code></pre>

<p>but wouldn't work for the other cases. Any help would be much appreciated, I realize that one could solve it without regex, maybe just with the str.split() method, but unsure of how to proceed in a pythonic and/or pandas way. </p>

<p>try this:</p>

<pre><code>In [31]: df.name.str.extract(r'\b(?:UserId|loginId)\s*:\s*\b([^\s]+)\b', expand=True)
Out[31]:
            0
0   Z5QF1X33A
1   test.user
2  0000012348
3   Z5QF1X33A
</code></pre>

python regex string pandas split