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