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

18 Apr, 2022
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 callable 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”. 
Python3
# importing pandas moduleimport pandas as pd # reading csv file from urldata = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv") # overwriting column with replaced value of agedata["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 columnsdata.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. 
Python3
# importing pandas moduleimport pandas as pd # reading csv file from urldata = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv") # overwriting column with replaced value of agedata["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 columnsdata.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. 
varshagumber28
Python pandas-series
Python pandas-series-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Convert integer to string in Python
How to drop one or multiple columns in Pandas Dataframe