replacing a column in DataFrame using regex
<p>I have a data frame with 4 columns, col4 is a string including texts and digits:</p>

<pre><code> Col1           Col2              Col3           Col4
Syslog        2016,09,17           1    PD380_003 %LINK-3-UPDOWN
Syslog        2016,09,17           1    NM380_005 %BGP-5-NBR_RESET
Syslog        2016,09,14           1    NM380_005 %BGP-5-NBR_RESET
Syslog        2016,09,08           1    DO NOT TICKET LO380_004 %SYS-5-CONFIG_I Config
</code></pre>

<p>i need to keep a substring of that column and delete anything else, so I used regex and i made a pattern but when i run the following query the result is not what i want, it replace everything with the pattern itself:</p>

<pre><code>data.replace({'Col4':{'.*':'([A-Z]{2}[0-9]{3}_[0-9]{3})'}},regex=True)
</code></pre>

<p>desired result is:</p>

<pre><code> Col1           Col2              Col3           Col4
Syslog        2016,09,17           1           PD380_003
Syslog        2016,09,17           1           NM380_005
Syslog        2016,09,14           1           LO380_004
Syslog        2016,09,08           1           LO380_004
</code></pre>

<p>but the result i get is like:</p>

<pre><code> Col1           Col2              Col3           Col4
Syslog        2016,09,17           1    ([A-Z]{2}[0-9]{3}_[0-9]{3})
Syslog        2016,09,17           1    ([A-Z]{2}[0-9]{3}_[0-9]{3})
Syslog        2016,09,14           1    ([A-Z]{2}[0-9]{3}_[0-9]{3})
Syslog        2016,09,08           1    ([A-Z]{2}[0-9]{3}_[0-9]{3})
</code></pre>

<p>what am i doing wrong?</p>

<p>You were using RegEx in a wrong way.</p>

<p><code>{'Col4':{'.*':'([A-Z]{2}[0-9]{3}_[0-9]{3})'}}</code> - means replace a whatever string in the <code>Col4</code> column with <code>'([A-Z]{2}[0-9]{3}_[0-9]{3})'</code></p>

<p>Try this:</p>

<pre><code>In [87]: df.replace({'Col4':{r'.*?([A-Z]{2}[0-9]{3}_[0-9]{3}).*':r'\1'}}, regex=True)
Out[87]:
     Col1        Col2  Col3       Col4
0  Syslog  2016,09,17     1  PD380_003
1  Syslog  2016,09,17     1  NM380_005
2  Syslog  2016,09,14     1  NM380_005
3  Syslog  2016,09,08     1  LO380_004
</code></pre>

python pandas