How to Use str.replace in Pandas (With Examples)


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

Featured Posts

Leave a Reply

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