You could use Series.str.replace:
import pandas as pd
df = pd.DataFrame(['$40,000*','$40000 conditions attached'], columns=['P'])
print(df)
# P
# 0 $40,000*
# 1 $40000 conditions attached
df['P'] = df['P'].str.replace(r'\D+', '', regex=True).astype('int')
print(df)
yields
P
0 40000
1 40000
since \D matches any character that is not a decimal digit.
You could use Series.str.replace:
import pandas as pd
df = pd.DataFrame(['$40,000*','$40000 conditions attached'], columns=['P'])
print(df)
# P
# 0 $40,000*
# 1 $40000 conditions attached
df['P'] = df['P'].str.replace(r'\D+', '', regex=True).astype('int')
print(df)
yields
P
0 40000
1 40000
since \D matches any character that is not a decimal digit.
You could use pandas' replace method; also you may want to keep the thousands separator ',' and the decimal place separator '.'
import pandas as pd
df = pd.DataFrame(['$40,000.32*','$40000 conditions attached'], columns=['pricing'])
df['pricing'].replace(to_replace="\$([0-9,\.]+).*", value=r"\1", regex=True, inplace=True)
print(df)
pricing
0 40,000.32
1 40000
First, you have the wrong regex's in the wrong positions. The to_replace argument to .replace needs to match what to replace and what to delete. So you need a ^.* in front of and a .*$ behind your regex in this case since you want to trim the string outside the match:
^.*([A-Z]{2}[0-9]{3}_[0-9]{3}).*$
Demo
Second, the replace argument, if a regex, needs to be a capturing group or fixed string. In this case \1 will do.
Last, the Series form of .replace has a littler simpler syntax (at least for me) to understand.
So given:
>>> df
Col1 Col2 Col3 Col4
0 SysLog 2016,09,17 1 PD380_003 %LINK-3-UPDOWN
1 SysLog 2016,09,17 1 NM380_005 %BGP-5-NBR_RESET
2 SysLog 2016,09,17 1 NM380_005 %BGP-5-NBR_RESET
3 SysLog 2016,09,17 1 DO NOT TICKET LO380_004 %SYS-5-CONFIG_I Config
You can do:
>>> df['Col4'].replace(to_replace='^.*([A-Z]{2}[0-9]{3}_[0-9]{3}).*$', value=r'\1', regex=True)
0 PD380_003
1 NM380_005
2 NM380_005
3 LO380_004
Name: Col4, dtype: object
You can also use a positional argument version if easier:
df['Col4'].replace('^.*([A-Z]{2}[0-9]{3}_[0-9]{3}).*$', r'\1', regex=True)
but you need to have regex=True since the replacement string is to be interpreted as a regex -- not just a static string.
Finally, assign directly into the original:
>>> df['Col4']=df['Col4'].replace('^.*([A-Z]{2}[0-9]{3}_[0-9]{3}).*$', r'\1', regex=True)
>>> df
Col1 Col2 Col3 Col4
0 SysLog 2016,09,17 1 PD380_003
1 SysLog 2016,09,17 1 NM380_005
2 SysLog 2016,09,17 1 NM380_005
3 SysLog 2016,09,17 1 LO380_004
I think you need extract:
data.Col4 = data.Col4.str.extract('([A-Z]{2}[0-9]{3}_[0-9]{3})', expand=False)
print (data)
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