In pandas, we can use the str.replace() function to replace text in a Series or column in a Dataframe. The str.replace() function allows us to perform a string search or regular expression (regex) search on the elements in a Series or column and replace them.


From the pandas documentation, the pandas str.replace() function takes 6 parameters:

def replace(
        pat: str | re.Pattern,
        repl: str | Callable,
        n: int = -1,
        case: bool | None = None,
        flags: int = 0,
        regex: bool | None = None,

The first parameter is the pattern we want to search for. This pattern can be a literal string, or a regex pattern. The second parameter is what we will replace the first parameter with.

The third parameter is the maximum number of replacements you want to perform from the start. The fourth parameter is if you want the search to be case sensitive or not.

The fifth parameter is for any regex module flags, and the sixth parameter is to specify if you the pattern you are searching for is a regular expression or not.

Using str.replace() to Replace a String in a Series with pandas

We can use the pandas preg_replace() function to replace a letters or words in a Column or Series of strings based on a pattern very easily in our python code.

Let’s say I have the following Series:

ser = pd.Series(["This","is","a","series","with","some","strings"])

Let’s say we want to replace the word “This” with “Here”. We can do that easily with the following use of str.replace() in our python code.

ser = pd.Series(["This","is","a","series","with","some","strings"])


0       Here
1         is
2          a
3     series
4       with
5       some
6    strings
dtype: object

Using str.replace() to Replace a Pattern in a Series with pandas

We can also use the str.replace() function to replace a regular expression pattern in a series with pandas.

Let’s say we have the same Series as above and want to replace all of the 4 letter words with the word “four”.

The regular expression for all 4 letter words is show below.

pattern = r'(?

We can pass this it the str.replace() function. Also, we need to pass the "regex=True" parameter to the function to make sure it works correctly.

ser = pd.Series(["This","is","a","series","with","some","strings"])
pattern = r'(?

Hopefully this article has been helpful for you to understand how you can use the pandas str.replace() function to replace strings with other strings based on strings and regex patterns in your Python code.

Categorized in:


Last Update: March 20, 2024