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.

Answer from unutbu on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.replace.html
pandas.DataFrame.replace — pandas 3.0.5 documentation
Value to replace any values matching to_replace with. For a DataFrame a dict of values can be used to specify which value to use for each column (columns not in the dict will not be filled). Regular expressions, strings and lists or dicts of such objects are also allowed. ... If True, performs operation inplace. regexbool or same types as to_replace, default False
🌐
pandas
pandas.pydata.org › pandas-docs › dev › reference › api › pandas.DataFrame.replace.html
pandas.DataFrame.replace — pandas 3.1.0.dev0 documentation
Value to replace any values matching to_replace with. For a DataFrame a dict of values can be used to specify which value to use for each column (columns not in the dict will not be filled). Regular expressions, strings and lists or dicts of such objects are also allowed. ... If True, performs operation inplace. regexbool or same types as to_replace, default False
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › replace-values-in-pandas-dataframe-using-regex
Replace Values in Pandas Dataframe using Regex - GeeksforGeeks
October 9, 2025 - Explanation: The regex [nN]ew matches both "New" and "new", replacing them with "New_" across the entire DataFrame column. The apply() function lets you define a custom function that uses Python’s re module for pattern matching and string replacement.
🌐
Machine Learning Plus
machinelearningplus.com › blog › regex replace values using pandas
RegEx Replace values using Pandas - machinelearningplus
March 8, 2022 - These may include retrieving hashtags from a tweet, extracting dates from a text, or removing website links. Pandas replace() function is used to replace a string regex, list, dictionary, series, number in a dataframe.
🌐
DataScientYst
datascientyst.com › replace-values-regex-pandas
How to replace values with regex in Pandas
December 2, 2021 - In this quick tutorial, we'll show how to replace values with regex in Pandas DataFrame. There are several options to replace a value in a column or the whole DataFrame with regex: 1. Regex replace string df['applicants'].str.replace(r'\sapplicants', '') 2. Regex replace capture group df['applicants']
Find elsewhere
🌐
datagy
datagy.io › home › pandas tutorials › pandas dataframes › pandas replace() – replace values in pandas dataframe
Pandas replace() - Replace Values in Pandas Dataframe • datagy
March 2, 2023 - In this post, you’ll learn how to use the Pandas .replace() method to replace data in your DataFrame. The Pandas DataFrame.replace() method can be used to replace a string, values, and even regular expressions (regex) in your DataFrame.
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › DataFrame › replace
Python Pandas DataFrame replace() - Replace Values | Vultr Docs
December 27, 2024 - Use replace() to substitute NA with a predetermined value. ... Here, all None entries (Pandas' representation of NA) are replaced with 0. This is especially useful in preparing datasets for machine learning models which require no missing values.
🌐
Linux find Examples
queirozf.com › entries › pandas-dataframe-replace-examples
Pandas Dataframe: Replace Examples
October 4, 2020 - Original dataframe · Use a dict to specify multiple replacements · Use df.replace(pattern, replacement, regex=True) import pandas as pd df = pd.DataFrame({ 'name':['john','mary','paul'], 'age':[30,25,40], 'city':['new york','los angeles','london'] }) df.replace('jo.+','FOO',regex=True) Original dataframe ·
🌐
IncludeHelp
includehelp.com › python › pandas-applying-regex-to-replace-values.aspx
Python - Pandas applying regex to replace values
# Importing pandas package import pandas as pd # Creating a dictionary d = {'Col':['$1100,000*','$40000 string created']} # Creating a dataframe df = pd.DataFrame(d) # Display Dataframe print("DataFrame :\n",df,"\n") # Using regex comparison df['Col'] = df['Col'].str.replace(r'\D+', '', regex=True).astype('int') # Display modified DataFrame print("Modified DataFrame:\n",df)
🌐
DataScience Made Simple
datasciencemadesimple.com › home › regular expression replace of substring of a column in pandas python
Regular expression Replace of substring of a column in pandas python - DataScience Made Simple
November 15, 2019 - Regular expression Replace of substring of a column in pandas python can be done by replace() function with Regex argument. Let’s see how to · Replace a pattern of substring with another substring using regular expression.
🌐
GeeksforGeeks
geeksforgeeks.org › data analysis › python-pandas-dataframe-replace
Python | Pandas dataframe.replace() - GeeksforGeeks
Syntax: DataFrame.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method='pad', axis=None) ... to_replace : [str, regex, list, dict, Series, numeric, or None] pattern that we are trying to replace in dataframe.
Published: July 11, 2025
🌐
Data Science Parichay
datascienceparichay.com › home › blog › pandas – replace values in a dataframe
Pandas - Replace Values in a DataFrame - Data Science Parichay
April 27, 2022 - To replace values within a dataframe via a regular expression match, pass regex=True to the replace function. Keep in mind that you pass the regular expression string to the to_replace parameter and the value to replace the matches to the value ...
Top answer
1 of 3
3

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
2 of 3
3

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
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.1 › reference › api › pandas.DataFrame.replace.html
pandas.DataFrame.replace — pandas 2.1.4 documentation
Values of the Series/DataFrame are replaced with other values dynamically. This differs from updating with .loc or .iloc, which require you to specify a location to update with some value. ... How to find the values that will be replaced. ... First, if to_replace and value are both lists, they must be the same length. Second, if regex=True then all of the strings in both lists will be interpreted as regexs otherwise they will match directly.
🌐
Javatpoint
javatpoint.com › pandas-replace
Pandas DataFrame.replace()
Pandas.replace() with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.