Python | Pandas Series.str.replace() to replace text in a series

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier.

Pandas Series.str.replace() method works like Python .replace() method only, but it works on Series too. Before calling .replace() on a Pandas series, .str has to be prefixed in order to differentiate it from the Python’s default replace method.

Syntax: Series.str.replace(pat, repl, n=-1, case=None, regex=True)

Parameters:
pat: string or compiled regex to be replaced
repl: string or callabe to replace instead of pat
n: Number of replacement to make in a single string, default is -1 which means All.
case: Takes boolean value to decide case sensitivity. Make false for case insensitivity
regex: Boolean value, if True assume that the passed pattern is a regex

Return Type: Series with replaced text values

To download the CSV used in code, click here.

In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.

Example #1: Replacing values in age column

In this example, all the values in age column having value 25.0 are replaced with “Twenty five” using str.replace()
After that, a filter is created and passed in .where() method to only display the rows which have Age = “Twenty five”.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module 
import pandas as pd
  
# reading csv file from url
  
# overwriting column with replaced value of age
data["Age"]= data["Age"].replace(25.0, "Twenty five")
  
# creating a filter for age column 
# where age = "Twenty five"
filter = data["Age"]=="Twenty five"
  
# printing only filtered columns
data.where(filter).dropna()

chevron_right


Output:
As shown in the output image, all the values in Age column having age=25.0 have been replaced by “Twenty five”.

 
Example #2: Case Insensitivity

In this example, team name Boston Celtics is replaced by New Boston Celtics. In the parameters, instead of passing Boston, boston is passed (with ‘b’ in lower case) and the case is set to False, which means case insensitive. After that only teams having team name “New Boston Celtics” are displayed using .where() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module 
import pandas as pd
  
# reading csv file from url 
  
# overwriting column with replaced value of age 
data["Team"]= data["Team"].str.replace("boston", "New Boston", case = False)
  
# creating a filter for age column 
# where age = "Twenty five" 
filter = data["Team"]=="New Boston Celtics"
  
# printing only filtered columns 
data.where(filter).dropna()

chevron_right


Output:
As shown in the output image, Boston is replaced by New Boston irrespective of the lower case passed in the parameters. This is because the case parameter was set to False.



My Personal Notes arrow_drop_up

Developer in day, Designer at night GSoC 2019 with Python Software Foundation (EOS Design system)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.