Often you may want to replace each occurrence of a particular pattern or substring in a pandas Series.
The easiest way to do so is by using the str.replace() function, which uses the following basic syntax:
Series.str.replace(pat, repl, n=-1, case=None, flags=0, regex=False)
where:
- pat: Pattern to replace
- repl: Replacement string to use
- n: Number of replacements to make from start
- case: Determine if replacement is case-sensitive
- flags: Regex module flags
- regex: Determine if pattern to replace is compiled regex
The following example shows how to use the str.replace() function in practice in a variety of different scenarios:
Example: How to Use str.replace() in Pandas
Suppose we create the following pandas DataFrame that contains information about various basketball players:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['Mavs', 'Cavs', 'cavs', 'Heat', 'Lakers', 'MAVS'], 'points': [18, 22, 19, 14, 40, 32], 'rebounds': [5, 7, 7, 9, 12, 17]}) #view DataFrame print(df) team points rebounds 0 Mavs 18 5 1 Cavs 22 7 2 cavs 19 7 3 Heat 14 9 4 Lakers 40 12 5 MAVS 32 17
The DataFrame contains three columns with the following information:
- team: The team name the player is on
- points: Total points scored by the player
- rebounds: Total rebounds collected by the player
Suppose that we would like to replace each occurrence of “Mavs” in the team column of the DataFrame with “Thunder” instead.
We can use the following syntax to do so:
#replace each occurrence of 'Mavs' with 'Thunder' in team column df['team'] = df['team'].str.replace('Mavs', 'Thunder') #view updated DataFrame print(df) team points rebounds 0 Thunder 18 5 1 Cavs 22 7 2 cavs 19 7 3 Heat 14 9 4 Lakers 40 12 5 MAVS 32 17
Notice that the team name “Mavs” has been replaced with “Thunder” in the first row of the team column.
However, the last row that contains “MAVS” has not been replaced because we did not specify that the replacement should be case-insensitive.
If we would like to perform a case-insensitive replacement, we can use the following syntax instead:
#replace each occurrence of 'Mavs' with 'Thunder' in team column (case-insensitive) df['team'] = df['team'].str.replace('Mavs', 'Thunder', case=False) #view updated DataFrame print(df) team points rebounds 0 Thunder 18 5 1 Cavs 22 7 2 cavs 19 7 3 Heat 14 9 4 Lakers 40 12 5 Thunder 32 17
By specifying case=False, we are able to replace each occurrence of “Mavs” in the team column with “Thunder”, regardless of case.
If you would like to replace multiple patterns with a new string, then you can use the | operator along with regex=True as follows:
#replace each occurrence of 'Mavs' and 'Cavs' with 'Thunder' in team column df['team'] = df['team'].str.replace('Mavs|Cavs', 'Thunder', regex=True) #view updated DataFrame print(df) team points rebounds 0 Thunder 18 5 1 Thunder 22 7 2 cavs 19 7 3 Heat 14 9 4 Lakers 40 12 5 Thunder 32 17
Notice that we are able to replace both “Mavs” and “Cavs” in the team column with the new value of “Thunder” instead.
Note that we must specify regex=True so that pandas knows that the expression used in the first argument of the str.replace() function is a regular expression, otherwise it will be interpreted as a literal string pattern to search for.
Note: You can find the complete documentation for the pandas str.replace() function here.
Additional Resources
The following tutorials explain how to perform other common tasks in pandas:
Pandas: Drop Columns if Name Contains Specific String
Pandas: How to Compare Strings Between Two Columns
Pandas: How to Extract Number from String
That’s really useful. I referred to your method and applied to my problem which worked out well! In case you wonder what my problem is, basically I have a dataframe with 4 columns and one of the columns contains cells with quite a bit of mixture of numbers and characters, and I need to extract numbers between position 19 – 24 only, and update this column in the dataframe. So what I did was:
df [‘columnname’] = df [‘columnname’].str[19:24].replace(‘columnname’, ‘columnname’)
And I got my updated column with the 4 digits I required together with other three columns unchanged.
Hi Xiao…That’s fantastic to hear, and well done on solving your problem! It seems like you’ve successfully extracted the numbers from the specified positions in your column and updated the DataFrame without affecting the other columns. However, I noticed a small inefficiency in your code that might not be doing exactly what you intended.
The line:
“`python
df[‘columnname’] = df[‘columnname’].str[19:24].replace(‘columnname’, ‘columnname’)
“`
It seems the `.replace()` part may not be needed unless you’re replacing specific values within those extracted substrings. The `.str[19:24]` already extracts the substring from position 19 to 24 for each row in the column, which seems to meet your requirement.
### Suggested Simplification
If your goal is simply to extract the substring from position 19 to 24 and update the column, you can simplify your code to:
“`python
df[‘columnname’] = df[‘columnname’].str[19:24]
“`
This will update the `’columnname’` column with just the extracted substrings while leaving the rest of the DataFrame unchanged.
### Additional Tip
If the content of `’columnname’` contains non-string values (e.g., `NaN` or integers), it’s a good idea to ensure all values are treated as strings before slicing:
“`python
df[‘columnname’] = df[‘columnname’].astype(str).str[19:24]
“`
This ensures there won’t be errors when applying `.str` methods to non-string data types.
—
Feel free to share if there are any additional complexities or enhancements you’d like to explore—I’m happy to help! 😊