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.
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)
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”.
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.
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.
- Python | Pandas Series.replace()
- Python | Pandas Timestamp.replace
- Python | Pandas dataframe.replace()
- Python | Pandas series.cumprod() to find Cumulative product of a Series
- Python | Pandas Series.cummin() to find cumulative minimum of a series
- Python | Pandas series.cummax() to find Cumulative maximum of a series
- Python | Pandas Series.astype() to convert Data type of series
- Python | Pandas Series.cumsum() to find cumulative sum of a Series
- Python | Pandas Series.mad() to calculate Mean Absolute Deviation of a Series
- Python | Pandas Series.nonzero() to get Index of all non zero values in a series
- Python | Pandas Series.get()
- Python | Pandas Series.agg()
- Python | Pandas Series.sem()
- Python | Pandas Series.last()
- Python | Pandas Series.dt.day
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.