Extract values from pandas stream
<p>I have very weird data coming via curl into my pandas dataframe. What I would like to do is extract values out of the column as described below. Can someone guide me how to extract the info?</p>

<pre><code>cc = pd.read_csv(cc_curl)
print(cc['srv_id'])

srv_id
------
TicketID 14593_ServiceID 104731
ServiceID 
TicketID 14595_ServiceID 104732
TicketID 14609_ServiceID 0
TicketID 0_ServiceID 178282
</code></pre>

<ol>
<li>Extract 5 digit ticket id and 6 digit service id.</li>
<li>Extract nothing since there is no ticketID and service ID is blank.</li>
<li>Extract 5 digit ticket id and 6 digit service id.</li>
<li>Extract 5 digit ticket id only and service id should be blank since it is 0.</li>
<li>Extract 6 digit service id only and leave ticket ID blank since it is 0.</li>
</ol>

<p>Desired output</p>

<pre><code>srv_id
------
14593 104731

14595 104732
14609
 178282
</code></pre>

<p>If you want to extract this information into two new columns, you can do it this way:</p>

<pre><code>import numpy as np
import pandas as pd

In [22]: df[['TicketID','ServiceID']] = (
    ...:   df.srv_id.str.extract(r'TicketID\s+(\d+).*?ServiceID\s+(\d+)', expand=True)
    ...:     .replace(r'\b0\b', np.nan, regex=True)
    ...: )
    ...:

In [23]: df
Out[23]:
                            srv_id TicketID ServiceID
0  TicketID 14593_ServiceID 104731    14593    104731
1                       ServiceID       NaN       NaN
2  TicketID 14595_ServiceID 104732    14595    104732
3       TicketID 14609_ServiceID 0    14609       NaN
4      TicketID 0_ServiceID 178282      NaN    178282
</code></pre>

<p>If you want to replace your string with extracted numbers:</p>

<pre><code>In [161]: df['new_srv_id'] = \
              df.srv_id.replace([r'[^\d{5,}]+', r'\s*\b0\b\s*'], [' ', ''], regex=True)

In [162]: df
Out[162]:
                            srv_id     new_srv_id
0  TicketID 14593_ServiceID 104731   14593 104731
1                       ServiceID
2  TicketID 14595_ServiceID 104732   14595 104732
3       TicketID 14609_ServiceID 0          14609
4      TicketID 0_ServiceID 178282         178282
</code></pre>

python python-3.x pandas dataframe