Often you may want to replace specific values in a pandas Series.
You can use the following methods to do so:
Method 1: Replace One Specific Value with Another Specific Value
teams.replace('Mavs', 'Spurs')
This particular example will replace all occurrences of the string Mavs with Spurs.
Method 2: Replace Multiple Specific Values with One Value
teams.replace(['Mavs', 'Magic'], 'Spurs')
This particular example will replace all occurrences of both the strings Mavs and Magic with Spurs.
Method 3: Replace Multiple Specific Values with Multiple Values
teams.replace({'Mavs': 'Spurs', 'Magic': 'Rockets'})
This particular example will replace all occurrences of Mavs with the string Spurs and all occurrences of Magic with the string Rockets.
The following examples show how to use each method in practice with the following pandas Series:
import pandas as pd #create pandas Series teams = pd.Series(['Mavs', 'Magic', 'Lakers', 'Mavs', 'Nets', 'Heat', 'Magic']) #view Series print(teams) 0 Mavs 1 Magic 2 Lakers 3 Mavs 4 Nets 5 Heat 6 Magic dtype: object
Example 1: Replace One Specific Value with Another Specific Value
We can use the following code to replace each occurrence of the string Mavs with Spurs in the pandas Series:
#replace each occurrence of 'Mavs' with 'Spurs' instead
teams.replace('Mavs', 'Spurs')
0 Spurs
1 Magic
2 Lakers
3 Spurs
4 Nets
5 Heat
6 Magic
dtype: object
Notice that both occurrences of Mavs in the Series have been replaced with Spurs instead.
Example 2: Replace Multiple Specific Values with Another Specific Value
We can use the following code to replace each occurrence of the strings Mavs and Magic with Spurs in the pandas Series:
#replace each occurrence of 'Mavs' and 'Magic' with 'Spurs' instead
teams.replace(['Mavs', 'Magic'], 'Spurs')
0 Spurs
1 Spurs
2 Lakers
3 Spurs
4 Nets
5 Heat
6 Spurs
dtype: object
Notice that all occurrences of the strings Mavs and Magic in the Series have been replaced with Spurs instead.
Note that you can use similar syntax to replace as many strings as you’d like with another specific string.
Simply include more values in the first comma-separated list of the replace() function to specify more values to replace.
Example 3: Replace Multiple Specific Values with Multiple Values
We can use the following code to replace each occurrence of the string Mavs with Spurs and each occurrence of the string Magic with Rockets in the pandas Series:
#make multiple specific replacements teams.replace({'Mavs': 'Spurs', 'Magic': 'Rockets'}) 0 Spurs 1 Rockets 2 Lakers 3 Spurs 4 Nets 5 Heat 6 Rockets dtype: object
Notice that the following replacements have been made:
- Each occurrence of Mavs has been replaced with Spurs.
- Each occurrence of Magic has been replaced with Rockets.
Note that we used a dictionary format within the replace() function to specify multiple replacements we would like to make.
In this particular example we made two specific replacements, but you can use similar syntax to make as many replacements as you’d like by adding more string combinations to the dictionary within the replace() function.
Additional Resources
The following tutorials explain how to perform other common tasks in Pandas:
How to Plot a Pandas Series
How to Iterate Over a Series in Pandas
How to Convert Pandas Series to DataFrame
How to Convert Pandas Series to NumPy Array