• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / pandas str replace – Replace Text in Dataframe with Regex Patterns

pandas str replace – Replace Text in Dataframe with Regex Patterns

December 28, 2021 Leave a Comment

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.

series.str.replace(r'/\s\s+/','new_text',regex=True)

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

def replace(
        self,
        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"])

ser.str.replace("This","Here")

#Output: 
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'(?<!\S)\S{4}(?!\S)'

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'(?<!\S)\S{4}(?!\S)'

ser.str.replace(pattern,"four",regex=True)

#Output: 
0       four
1         is
2          a
3     series
4       four
5       four
6    strings
dtype: object

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.

Other Articles You'll Also Like:

  • 1.  pandas mad – Calculate Mean Absolute Deviation in Python
  • 2.  Using Python to Reverse Tuple
  • 3.  Writing Multiple Lines Lambda Expression in Python
  • 4.  Read Last Line of File Using Python
  • 5.  Using Python to Split String by Newline
  • 6.  Using Python to Capitalize Every Other Letter of String
  • 7.  Get Day of the Year with Python
  • 8.  How to Check if Tuple is Empty in Python
  • 9.  Squaring in Python – Square a Number Using Python math.pow() Function
  • 10.  Using Selenium to Get Text from Element in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy