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



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

 

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”.

# 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()

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.

# 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()

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.

Last Updated on August 30, 2021 by admin

Leave a Reply

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

Recommended Blogs